iOS开发技巧,细节(二)
1.常量名稱最好用static標識,例如下面的代碼,包括其他一些只需要定義一次,之后不需要變化的變量也最好使用static
static NSString *CellIdentifier = @"Cell";
?
2.當設置視圖控制器需要接受通知時,需要在dealloc取消監聽
例如下面代碼監聽通知:
? ??[[NSNotificationCenter defaultCenter] addObserver:self
? ? ? selector:@selector(applicationDidEnterBackground)
? ? ? name:UIApplicationDidEnterBackgroundNotification
? ? ? object:nil];
則需要重寫dealloc方法:
- (void)dealloc
{
? [[NSNotificationCenter defaultCenter] removeObserver:self];
}
?
3.當出現EXC_BAD_ACCESS異常錯誤時,可用以下方法找出錯誤:
?
設置完以后,當app cash時,Xcode會指出錯誤的地方,一般是代碼語句順序有誤導致內存相關問題
?
4.向某個視圖控制器嵌入另一個視圖控制器的實現:
父視圖控制器中顯示子視圖控制器代碼,語句順序不能有錯:
? ??DetailViewController *controller = [[DetailViewController alloc] initWithNibName:@"DetailViewController" bundle:nil];
? ? [self.view addSubview:controller.view];
? ? [self addChildViewController:controller];
? ? [controller didMoveToParentViewController:self];
子視圖控制器DetailViewController從父視圖中移除的代碼,語句順序不能有錯:
? ??[self willMoveToParentViewController:nil];
? ? [self.view removeFromSuperview];
? ? [self removeFromParentViewController];
?
5.對于@""字符串最好以NSLocalizedString()形式出現,有助于后面進行國際化
轉載于:https://www.cnblogs.com/guitarandcode/p/5689873.html
總結
以上是生活随笔為你收集整理的iOS开发技巧,细节(二)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: DM365 u-boot启动分析
- 下一篇: 王莉:将开发文档英文化和本地化,我们努力