1.AFNetworking返回結(jié)果默認(rèn)回到主線程中惹恃,所以在AFNetworking的返回結(jié)果block中無需手動回歸主線程。
2.delegate盡量使用weak修飾,弱引用
3.block中的代碼不需要特別回歸注線程棺牧,設(shè)置block時在哪個線程座舍,block中的代碼就執(zhí)行在哪個線程
4.使用NSObject+MJKeyValue進(jìn)行模型轉(zhuǎn)換時,int類型可直接轉(zhuǎn)為string類型陨帆,原因不明曲秉,但可以正確的轉(zhuǎn)換過來
小技巧
新項目http不可使用
在info.plist 中加入NSAppTransportSecurity(NSDictionary)以及 NSAllowsArbitraryLoads(Bool)并設(shè)置為YES
delegate盡量使用weak修飾,弱引用
block中的代碼不需要特別回歸注線程,設(shè)置block時在哪個線程疲牵,block中的代碼就執(zhí)行在哪個線程
使用 MJExtension 進(jìn)行字典轉(zhuǎn)模型時時承二,如果模型對應(yīng)屬性為NSString
那么原始字典中對應(yīng)的鍵值對的value的類型是int或NSNumber類型
MJExtension內(nèi)部做了轉(zhuǎn)換操作。
基類常用Tips
//字符串截取纲爸,從第4個字符開始截取到末尾亥鸠,包含第4個字符
[a substringFromIndex:4];
//字符串截取,從開始截取到第4個字符识啦,不包含第4個字符
[a substringToIndex:4];
//字符串截取负蚊,從第1個字符,往后截2個字符長度
[a substringWithRange:NSMakeRange(1,2)]
//NSString轉(zhuǎn)NSArry
NSArray *array = [@"1,2,3,4,5" componentsSeparatedByString:@","];
//NSArry轉(zhuǎn)NSString
NSString *str = [@[@"1", @"2", @"3"] componentsJoinedByString:@","];
//判斷代理是否有接收者
if ([self.delegate respondsToSelector:@selector(callBack:)]) {
}
//異步線程切換主線程
dispatch_async(dispatch_get_main_queue(), ^{
//do something
});
//延時執(zhí)行某一步驟
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3 * NSEC_PER_SEC));
dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
[self.webView performSelector:@selector(loadRequest:) withObject:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:path]]];
});
電量欄導(dǎo)航欄相關(guān)
//電量欄文字改為白色顏色
// Info.plist 中 View controller-based status bar appearance設(shè)置為NO
[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
//設(shè)置全局NavgationBar背景圖片
[[UINavigationBar appearance] setBackgroundImage:image forBarMetrics:UIBarMetricsDefault];
//設(shè)置全局NavgationBar背景顏色
[[UINavigationBar appearance] setBarTintColor:[UIColor blueColor]];
//設(shè)置全局NavgationBar左右按鈕顏色
[[UINavigationBar appearance] setTintColor:[UIColor whiteColor]];
//設(shè)置全局NavgationBar標(biāo)題顏色
[[UINavigationBar appearance] setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor whiteColor]}];
//Navgation造成的位置偏移或遮擋(64像素問題)
if ([self respondsToSelector:@selector(setEdgesForExtendedLayout:)]){
self.edgesForExtendedLayout = UIRectEdgeNone;
}
//隱藏單個頁面的返回按鈕
self.navigationItem.hidesBackButton = YES;
//導(dǎo)航欄背景透明
[self.navigationController.navigationBar setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];
self.navigationController.navigationBar.shadowImage = [UIImage new];
//背景恢復(fù)原色
[self.navigationController.navigationBar setBackgroundImage:nil forBarMetrics:UIBarMetricsDefault];
self.navigationController.navigationBar.shadowImage = nil;
//Navgation完全隱藏
- (void)viewDidLoad {
[super viewDidLoad];
self.navigationController.delegate = self;
}
-(void)viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];
[self.navigationController setNavigationBarHidden:YES animated:YES];
}
//Navgation在下一頁面再顯示
- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated {
if (viewController != self && [navigationController isNavigationBarHidden]) {
[navigationController setNavigationBarHidden:NO animated:animated];
}
}
UI常用Tips
//從storyBoard中獲取UIViewController
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
UIViewController *viewController = [storyboard instantiateViewControllerWithIdentifier:@"UserInfoListViewController"];
//從Xib獲取UIViewController
RegistViewController *regist = [[RegistViewController alloc] initWithNibName:@"RegistViewController" bundle:nil];
//從storyBoard抽取自定義的UITableViewCell
CustCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CustomCellIdentifier"];
//UITableView刪除多余的橫線
self.tableView.tableFooterView = [[UIView alloc] init];
//UITableView的Cell自適應(yīng)
self.tableView.rowHeight = UITableViewAutomaticDimension;
self.tableView.estimatedRowHeight = 44.0;
//讓view保持在最上層
[self.view bringSubviewToFront:sonView];
//讓view保持在最下層
[self.view sendSubviewToBack:sonView];