1.AFNetworking返回结果默认回到主线程中,所以在AFNetworking的返回结果block中无需手动回归主线程。
2.delegate尽量使用weak修饰,弱引用
3.block中的代码不需要特别回归注线程,设置block时在哪个线程,block中的代码就执行在哪个线程
4.使用NSObject+MJKeyValue进行模型转换时,int类型可直接转为string类型,原因不明,但可以正确的转换过来
小技巧
新项目http不可使用
在info.plist 中加入NSAppTransportSecurity(NSDictionary)以及 NSAllowsArbitraryLoads(Bool)并设置为YES
delegate尽量使用weak修饰,弱引用
block中的代码不需要特别回归注线程,设置block时在哪个线程,block中的代码就执行在哪个线程
使用 MJExtension 进行字典转模型时时,如果模型对应属性为NSString
那么原始字典中对应的键值对的value的类型是int或NSNumber类型
MJExtension内部做了转换操作。
基类常用Tips
//字符串截取,从第4个字符开始截取到末尾,包含第4个字符
[a substringFromIndex:4];
//字符串截取,从开始截取到第4个字符,不包含第4个字符
[a substringToIndex:4];
//字符串截取,从第1个字符,往后截2个字符长度
[a substringWithRange:NSMakeRange(1,2)]
//NSString转NSArry
NSArray *array = [@"1,2,3,4,5" componentsSeparatedByString:@","];
//NSArry转NSString
NSString *str = [@[@"1", @"2", @"3"] componentsJoinedByString:@","];
//判断代理是否有接收者
if ([self.delegate respondsToSelector:@selector(callBack:)]) {
}
//异步线程切换主线程
dispatch_async(dispatch_get_main_queue(), ^{
//do something
});
//延时执行某一步骤
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]]];
});
电量栏导航栏相关
//电量栏文字改为白色颜色
// Info.plist 中 View controller-based status bar appearance设置为NO
[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
//设置全局NavgationBar背景图片
[[UINavigationBar appearance] setBackgroundImage:image forBarMetrics:UIBarMetricsDefault];
//设置全局NavgationBar背景颜色
[[UINavigationBar appearance] setBarTintColor:[UIColor blueColor]];
//设置全局NavgationBar左右按钮颜色
[[UINavigationBar appearance] setTintColor:[UIColor whiteColor]];
//设置全局NavgationBar标题颜色
[[UINavigationBar appearance] setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor whiteColor]}];
//Navgation造成的位置偏移或遮挡(64像素问题)
if ([self respondsToSelector:@selector(setEdgesForExtendedLayout:)]){
self.edgesForExtendedLayout = UIRectEdgeNone;
}
//隐藏单个页面的返回按钮
self.navigationItem.hidesBackButton = YES;
//导航栏背景透明
[self.navigationController.navigationBar setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];
self.navigationController.navigationBar.shadowImage = [UIImage new];
//背景恢复原色
[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自适应
self.tableView.rowHeight = UITableViewAutomaticDimension;
self.tableView.estimatedRowHeight = 44.0;
//让view保持在最上层
[self.view bringSubviewToFront:sonView];
//让view保持在最下层
[self.view sendSubviewToBack:sonView];
网友评论