zoukankan      html  css  js  c++  java
  • UIViewController中的loadView方法

    一、死循环(self.view为nil

    @implementation ViewController
    
    - (void)loadView{
        NSLog(@"loadView.......");
    }
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        NSLog(@"viewdidLoad.....");
        UIView *selfView = self.view;
    }
    
    - (void)didReceiveMemoryWarning {
        [super didReceiveMemoryWarning];
    }
    
    @end

    运行结果是:

    2016-02-29 11:54:42.778 Test6[849:84362] loadView.......

    2016-02-29 11:54:42.925 Test6[849:84362] viewdidLoad.....

    ...........循环出现

    原因分析:因为UIViewController没有对应的nib文件,因此会进入loadView方法,然而loadView没有实例化self.view,进入viewDidLoad后用到self.view对象,由于self.view为nil,导致调用loadView方法。所以一直在loadView和viewDidLoad两个方法中循环执行。

    二、系统UIViewController类提供的loadView功能

    当存在nib时,执行nib;

    当不存在nib时,创建一个frame为(0,0,width,height)的视图。

    三、UIViewController实例化self.view的方式

    1、创建nib文件,在nib中设置view;

    2、没有nib文件时,不重写loadView方法,默认使用父类的loadView创建一个frame为(0,0,width,height)的视图。

    3、没有nib文件时,重写loadView方法,将自定的UIView赋值“=”给self.view

    上面三种方式,缺一不可!!!

    四、iOS9前后

    在iOS9之前,系统为UIViewController自动创建的view的frame为(0,20,width,height),是因为用的是[UIScreen applicationFrame]方法。

    但是iOS9之后,[UIScreen applicationFrame]方法被弃用,使用的是[UIScreen bounds]方法。因此frame为(0,0,width,height)。

  • 相关阅读:
    苹果一体机发射Wi-Fi
    iphone 屏蔽系统自动更新,消除设置上的小红点
    data parameter is nil 异常处理
    copy与mutableCopy的区别总结
    java axis2 webservice
    mysql 远程 ip访问
    mysql 存储过程小问题
    mysql游标错误
    is not writable or has an invalid setter method错误的解决
    Struts2中关于"There is no Action mapped for namespace / and action name"的总结
  • 原文地址:https://www.cnblogs.com/zhouyi-ios/p/5227069.html
Copyright © 2011-2022 走看看