【iOS小筆記】

1.快速打開當前工程的某個文件 —— shift+command+o



1.某一類庫方法中有關:

@required  這個是必須的

@optional  這個是可選的


2.在info裏面添加一個語言

Localizations ——》Language     

然後在Supporting Files文件夾裏的--》

InfoPlist.strings(english)裏添加:

CFBundleDisplayName = "English Name";

InfoPlist.strings(chinese)裏添加:

CFBundleDisplayName = "中文名稱";

則即爲此應用的中文名和英文名


3.添加程序啓動圖片:直接加入並命名爲:“defualt.png”即默認圖片。

添加應用logo:直接加入圖片並命名爲:“icon.png”


4.給頂部navigation設置風格,要寫在viewdidload方法裏,在init方法不行

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil

{

    self = [superinitWithNibName:nibNameOrNilbundle:nibBundleOrNil];

    if (self) {

        self.tabBarItem.title =NSLocalizedString(@"First",@"First");

        self.tabBarItem.image = [UIImageimageNamed:@"first"];

        self.navigationItem.title =NSLocalizedString(@"Schedule",@"Schedule");

        self.navigationItem.leftBarButtonItem = [[UIBarButtonItemalloc]initWithBarButtonSystemItem:UIBarButtonSystemItemEdittarget:selfaction:@selector(edit:)];

        self.navigationItem.rightBarButtonItem = [[UIBarButtonItemalloc]initWithBarButtonSystemItem:UIBarButtonSystemItemAddtarget:selfaction:@selector(add:)];

        self.navigationController.navigationBar.barStyle =UIBarStyleBlackOpaque;

    }

    returnself;

}

推進下一視圖:

 [[selfnavigationController]pushViewController:view2animated:YES];

  [selfpresentModalViewController:navanimated:YES];


5.可變數組使用前必須先申請空間,不建議直接賦值,否則會出現詭異錯誤!


6.二級類給一級類傳值可用兩種方法

【1】直接調用按照子視圖所在類調用其成員方法

 [[[self.presentingViewControllerchildViewControllers]objectAtIndex:0]reloadarr];

 [selfdismissModalViewControllerAnimated:YES];

【2】利用委託機制

二級類定義一個委託,一級類調用

22.h

@protocol showBookDelegate <NSObject>

-(NSString *)bookListRequest;

@end

@property (nonatomic,assign)id<showBookDelegate> delegate;

[delegatebookListRequest];

11.h

@interface 11 :UIViewController<showBookDelegate>

22.delegate =self;


7.數組內部排序

 [_array sortUsingComparator:^NSComparisonResult(id obj1,id obj2) {

        return [obj1compare:obj2]; }];


8.ScrollView

//設置參數

CGRect frame = [[UIScreen mainScreen]applicationFrame];

//初始化此參數下的ScrollView位置

scrollView = [[UIScrollView alloc]initWithFrame:frame];

//帶位置及大小參數初始化

scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0.0f, 0.0f, 320.0f, 480.0f)];

[scrollView  setFrame:CGRectMake(0.0f, 0.0f, 320.0, 480.0)];

//設置其大小參數

scrollView .contentSize = CGSizeMake(320.0f,scrollView .frame.size.height);  

//設置scrollView的大小和image大小一致 

scrollView.contentSize = image.size;

//允許按照屏幕大小進行翻頁  

scrollView .pagingEnabled = YES;

//允許滾動至邊緣並出現view的背景

scrollView .bounces = YES;

//把對象添加到scrollView上

[scrollView  addSubview:msv];

//把scrollView添加到view上

[self.view addSubview:scrollView ];

//允許上下拖動

scrollView.alwaysBounceVertical = YES;  

//允許左右拖動

scrollView.alwaysBounceHorizontal = YES;   

//放小至圖片的一半

scrollView.minimumZoomScale = 0.5;   

//放大至圖片的二倍

scrollView.maximumZoomScale = 2.0;   

//允許放大縮小

scrollView.bouncesZoom = YES;

//設置委託

scrollView .delegate = self;


9.委託概念

我上班的工作主要內容包括 [1]寫代碼 [2]寫文檔 [3]測試程序 [4]接電話 [5]會見客戶

[1][2]我自己全權負責,但是後面[3][4][5]我不想或者不方便自己做,所以我想找個助手(delegate)幫我做這些事,於是我定了一個招聘要求(Protocol),裏寫明我的助手需要會做[3][4][5]這三件事。很快,我招到一個助手。

.delegate = 助手;

於是以後每當我遇到需要測試程序或者接電話的活,我就把他轉交給助手(delegate)去處理,助手處理完後如果有處理結果(返回值)助手會告訴我,也許我會拿來用。如果不需要或者沒有結果,我就接着做下面的事。。


10.屬性.參數格式的使用方法   

聲明:@property (修飾)類型屬性名

@property(nonatomic,retain) id _delegate;

實現:@synthesize屬性名

@synthesize -delegate;

修飾的類型:

readwrite: 變量可讀寫 ,生成getset方法

readonly: 變量只可以讀,需要手動添加setter,只有get方法(與assign無法同時選擇)

assign:變量可以直接賦值,內存引用計數值不變,生成簡單的封裝,默認值爲他,所有基本類型用他

retain: 對象進行一次引用,內存引用計數值+1

copy: 複製一個新的對象,內存引用計數值+1,複製一份使用(實質爲引用計數加一,指向的是同一內存地址,而不是真正意義上的複製),一般字符串使用(官方例子中只有字符串有用)

nonatomic: 默認爲atomic,在多線程是變量互斥,對象在不使用多線程時使用,提高性能


11.在NSdictionary中建議只用objectforkey就好了。


12.取文件內容

 NSDictionary *data = [[NSDictionaryalloc]initWithContentsOfFile:[[NSBundlemainBundle]pathForResource:@"Points"ofType:@"plist"]];


13.在tabbar邊緣添加數字

[self.tabBarItemsetBadgeValue:[NSStringstringWithFormat:@"%d",1]];

- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController

{

    NSLog(@"%@",self.tabBarItem.badgeValue);

    self.tabBarItem.badgeValue=0;

    [self.tabBarItemsetBadgeValue:[NSStringstringWithFormat:@"%d",0]];

}


14.內存管理

1.靜態分析 

     靜態分析就是使用Xcode自帶的Analyze功能(Product-> Analyze),對代碼進行靜態分析,對於內存泄露(Potential Memory Leak,未使用局部變量(dead store),邏輯錯誤(Logic Flaws)以及API使用問題(API-usage)等明確的展示出來

Dead store 從未實用過的變量        刪除即可

Memory Leak 的提示其實也比較好辦,因爲其提示已經足夠詳細,具體到了具體泄露的代碼行。找到問題對症下藥即可,忘記release的加上release,該放autorelease的地方加上auto release


2。動態分析

    InstrumentsXcode自帶的一個強大的應用分析工具,其功能並不侷限於內存泄露的分析上,內存佔用、CPU使用率等都是其分析的對象

        連接實體機到開發機,選擇選擇運行設備爲設備,點擊Products-> Profile,經過編譯後彈出一個Choose Trace Template or Existing Docunment雙擊leak進入下一級頁面

自行摸索

先靜態分析下,把能結局的內存泄漏隱患都給消除了,然後再動態分析


15.屏幕截圖

UITapGestureRecognizer *tapGestureRecognizer2 = [[UITapGestureRecognizeralloc]initWithTarget:selfaction:@selector(takeScreenshot:)];

tapGestureRecognizer2.numberOfTapsRequired =2;

UITapGestureRecognizer *tapGestureRecognizer3 = [[UITapGestureRecognizeralloc]initWithTarget:selfaction:@selector(takeScreenshot:)];

tapGestureRecognizer3.numberOfTapsRequired =3;

[tapGestureRecognizer2 requireGestureRecognizerToFail:tapGestureRecognizer3];

[self.viewaddGestureRecognizer:tapGestureRecognizer2];

[self.viewaddGestureRecognizer:tapGestureRecognizer3];


- (void) takeScreenshot:(UITapGestureRecognizer *)gestureRecognizer

{

CGFloat scale = gestureRecognizer.numberOfTapsRequired -1.0f;

UIGraphicsBeginImageContextWithOptions(self.inputAccessoryView.frame.size,NO, scale);

CGContextRef context =UIGraphicsGetCurrentContext();

[self.inputAccessoryView.layerrenderInContext:context];

UIImage *screenshot =UIGraphicsGetImageFromCurrentImageContext();

UIGraphicsEndImageContext();

NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES)lastObject];

[[NSFileManagerdefaultManager]createDirectoryAtPath:documentsDirectorywithIntermediateDirectories:YESattributes:nilerror:NULL];

NSString *scaleString = scale !=1.f ? [NSStringstringWithFormat:@"@%gx", scale] :@"";

NSString *screenshotName = [NSStringstringWithFormat:@"%@%@.png", [[selfinputAccessoryView]class], scaleString];

NSString *screenshotPath = [documentsDirectorystringByAppendingPathComponent:screenshotName];

[UIImagePNGRepresentation(screenshot)writeToFile:screenshotPathatomically:YES];

system([[NSStringstringWithFormat:@"open \"%@\"", screenshotPath]fileSystemRepresentation]);

}


16.過渡動畫

[UIViewbeginAnimations:@"up"context:nil];

[UIViewsetAnimationDuration:1.0];

[self.viewsetFrame:CGRectMake(0, -190,320,460)];

[UIViewcommitAnimations];

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章