您的当前位置:首页正文

【iOS】开发过程中遇到的问题

来源:要发发知识网

总结开发过程中遇到的各种问题,如有理解错误,欢迎指正,谢谢!持续更新中。。。

  • 从“A”ViewController Push到“B”ViewController时动画效果出现卡顿。
    原因:ViewController没有设置背景色,默认self.view的背景色是透明的,由于透明色颜色重叠后导致视觉上出现的卡顿问题。
    解决方法:给“B”ViewController设置背景色:
    self.view.backgroundColor = [UIColor whiteColor];

  • 控制器的automaticallyAdjustsScrollViewInsets问题
    原因:automaticallyAdjustsScrollViewInsets是iOS7.0之后出现的属性,默认为YES,它会根据所在界面的status bar, search bar, navigation bar, toolbar, or tab bar的高度,自动调整scrollview的 inset。
    解决方法:设置控制器的该属性为NO:
    self.automaticallyAdjustsScrollViewInsets = NO;

  • 从“B”ViewController pop出去时,内存不释放问题。
    说明:内存不释放有几个原因:
    1、代码中造成循坏引用;
    2、部分内存需要手动释放;如NSTimer、CADisplayLink、通知中心都需要手动释放
    解决方法:避免造成循环引用;手动释放需释放的代码

//释放NSTimer
[self.timer invalidate];
self.timer = nil;
//释放CADisplayLink
[self.displayLink invalidate];
self.displayLink = nil;
//释放通知中心(谁注册谁释放)
-(void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}
  • 蓝牙开发中,在“B”ViewController创建[[CBCentralManager alloc] initWithDelegate:self queue:nil];,pop回“A”ViewController后关闭蓝牙导致程序crash问题。
    说明:pop之后,“B”ViewController释放掉了,但是在“A”ViewController中关闭蓝牙的时候依然会调用centralManagerDidUpdateState:方法,该方法已经被释放掉了导致程序找不到该方法所以程序崩溃。
    解决方法:可以将蓝牙相关模块封装成一个单例类。

  • ios 点击子视图的时候会响应父视图的点击事件
    解决方法:
    1、让你的类继承 UIGestureRecognizerDelegate
    2、tap.delegate = self;
    3、实现gestureRecognizer:shouldReceiveTouch:方法:

//Object-C:
-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {
    if ([touch.view isDescendantOfView:subView])  {
        return NO;
    }
    return YES;
} 
//Swift:
func gestureRecognizer(gestureRecognizer: UIGestureRecognizer, shouldReceiveTouch touch: UITouch) -> Bool  {
    if touch.view.isDescendantOfView(subView){
        return false
    }
    return true
}
  • Version( 应用程序发布版本号 )和 Build( 应用程序内部标示 )
    1、Version对应的是CFBundleShortVersionString。
    Version 一般由产品部门确定,版本号是由分隔的整数组成的字符串,一般有2段或者3段式, 如:1.2, 1.2.3
    二段式:
    第一个段:(主版本号)大功能的新增或者有迥异的变化
    第二个段:(副版本号)既包含小功能更新也会包含 bug 修复
    三段式:
    第一个段:重大修改的版本,如实现新的大功能或重大变化的修订。
    第二个段:实现较突出的特点,如新功能添加和大问题修复。
    第三个段:代表维护版本,修复bug。
    2、Build( 应用程序内部标示 )
    Bulid 是给内部使用,与 Version 不会有太大联系.
    Bulid对应的是CFBundleVersion。标识(发布或未发布)的内部版本号。用以记录开发版本的,每次更新的时候都需要比上一次高。
    作用:发布build版本供测试团队进行测试。
//Object-C
NSDictionary *info= [[NSBundle mainBundle] infoDictionary];
info[@"CFBundleShortVersionString"]; //Version
info[@"CFBundleVersion"]; //Build
//Swift
let info:NSDictionary! = NSBundle.mainBundle().infoDictionary
let version = info["CFBundleShortVersionString"];
let build = info["CFBundleVersion"];
print("version = \(version),build = \(build)")
  • Xcode新建playground报错:Unable to find execution service for selected run destination
    解决方法:关闭Xcode,在终端执行两行代码
rm -rf ~/Library/Developer/CoreSimulator/Devices
killall -9 com.apple.CoreSimulator.CoreSimulatorService