iOS开发中遇到细节问题总结(持续更新...)

  1.MRC 和 ARC 下的可变数组的懒加载

  MRC 和 ARC 下可变数组的懒加载,对数组的初始化方式不同:

  ARC 下:

1 - (NSMutableArray *)lazyMutableArray {
2     if (!_lazyMutableArray) {
3         _lazyMutableArray = [NSMutableArray array];
4     }
5     return _lazyMutableArray;
6 }

  MRC 下:

1 - (NSMutableArray *)lazyMutableArray {
2     if (!_lazyMutableArray) {
3         _lazyMutableArray = [[NSMutableArray alloc] init];
4     }
5     return _lazyMutableArray;
6 }

  在 MRC 下应该用 [[NSMutableArray alloc] init],用 alloc 初始化,数组的引用计数会加 1(使用 [NSMutableArray array] 引用计数不会加 1 ),防止数组提前释放。

 

  2.使用 NSURLSession 请求数据,请求完成的处理

  使用 NSURLSession 请求数据,在请求完成的回调方法里面 - (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didCompleteWithError:(NSError *)error {} ,数据处理完毕需要调用 [session finishTasksAndInvalidate],不然会有内存泄漏,影响控制器的释放,导致 delloc 方法不会执行。

参考链接:http://blog.csdn.net/hncsy403/article/details/53096071

  3.UINavigationItem 的两个属性 leftBarButtonItem 和 backBarButtonItem

1 @property(nullable, nonatomic,strong) UIBarButtonItem *leftBarButtonItem;
2 
3 @property(nullable,nonatomic,strong) UIBarButtonItem *backBarButtonItem __TVOS_PROHIBITED; // Bar button item to use for the back button in the child navigation item.

  1.对当前页面而言,leftBarButtonItem 的优先级最高,如果对当前页面的 self.navigationItem.leftBarButtonItem 赋新值,则当前页面每次被 push 进来左上角显示的就是这个新赋的 UIBarButtonItem。

  2.如果当前页面的 self.navigationItem.leftBarButtonItem 没有赋新值,但是 push 当前页面进来的上个页面的 self.navigationItem.backBarButtonItem 重新赋值了,则当前页面的左上角显示上个页面的 self.navigationItem.backBarButtonItem。

  3.如果当前页面的 leftBarButtonItem 和上个页面的 backBarButtonItem 都没有赋新值,则当前页面的左上角显示默认的返回按钮,有一个向左的箭头,文字是上一个页面的导航栏 title。

  注意:针对第 2 种情况,给 self.navigationItem.backBarButtonItem   赋新值的时候,创建 UIBarButtonItem 时,使用:

1  - (instancetype)initWithBarButtonSystemItem:(UIBarButtonSystemItem)systemItem target:(nullable id)target action:(nullable SEL)action;
2 
3 - (instancetype)initWithCustomView:(UIView *)customView;

  这两个方法都是无效的,下个页面的左上角的返回按钮仍会按第 3 种情况的样式显示。

使用:

1 - (instancetype)initWithImage:(nullable UIImage *)image style:(UIBarButtonItemStyle)style target:(nullable id)target action:(nullable SEL)action;

  创建时,会同时显示两个图片,一个是系统的那个向左的箭头,一个是添加的图片。

  使用:

1 - (instancetype)initWithTitle:(nullable NSString *)title style:(UIBarButtonItemStyle)style target:(nullable id)target action:(nullable SEL)action;

  创建时,系统的那个向左的箭头依然显示,右边的文字显示的是创建时的 title。

  4.Hide Bottom Bar on Push

  先描述一下场景首先是在 storyboard 中做页面跳转,第一个页面是 UITabBarController 的 viewControllers ,它的下面有 UITabBarItem,然后这个页面上有一个按钮,点击按钮 Show(e.g.Push) 到一个 UIViewController,这个时候选中了 Xcode 右边 Attributes inspector 下的 HIde Bottom Bar on Push 选项,并暂时现在这个控制器的 View 上添加了一个 UIImageView,给它添加的约束是这样的,上下左右都是0,可是加完约束会发现这个 UIImageView 的底部并不与控制器的底部重合,会看到它们之间有一个 UITabbar 的高度差(49):

  这个时候运行代码,当点击跳转按钮的时候,Push 到这个有一个 UIImageView 的控制器显示完毕的时候,控制器的底部的 49 高度的白条向下隐藏消失,并且这个 UIImageView 也大概向下偏移 49,貌似这个控制器的 View 上的子控件重新进行了布局,这个 http://blog.csdn.net/ssirreplaceable/article/details/53032554 链接讲解的很细致,但是它的解决方法并不起作用,这时在这个 http://www.cocoachina.com/bbs/read.php?tid-332951.html 链接下发现了这个链接 http://stackoverflow.com/questions/28724255/auto-layout-and-hide-bottom-bar-when-pushed,在它里面找到了解决办法。

  这个时候删除 UIImageView 的底部的约束,同时选中 UIImageView 和控制器的 UIView,选中 Xcode 底部的 Align,添加这样一个约束:

  把它们的 Bottom Edges 设置为0,然后完美解决问题。

 

posted @ 2017-05-19 20:12  鳄鱼不怕牙医不怕  阅读(357)  评论(0编辑  收藏  举报