首页 > 移动平台 > 详细

ios view的创建过程

时间:2014-08-13 19:03:57      阅读:346      评论:0      收藏:0      [点我收藏+]

师傅发我一堆view的创建函数,目的让我知道view的创建过程。

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}
- (void)viewWillAppear:(BOOL)animated
{

}
- (void)viewDidAppear:(BOOL)animated
{

}
- (void)loadView
{

}
- (void)layoutSublayersOfLayer:(CALayer *)layer
{

}
- (void)viewWillLayoutSubviews
{

}
 - (void)viewDidLoad
{

}

拿到这函数我也没多想就创建了一个single View application 然后创建一个viewcontroller 就将师傅的函数复制进去,然后在AppDelegeteD.m文件中写

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc]initWithFrame:[[UIScreen mainScreen] bounds]];

 
    self.window.rootViewController = [[MyViewController alloc]init];
    
    [self.window makeKeyAndVisible];
    // Override point for customization after application launch.
    return YES;
}

然后运行   。。。。。。凉了。。。出现错误了;

启动起来黑屏,然后是报这样一个错误Application windows are expected to have a root view controller at the end of application launch

2014-08-13 15:16:45.924 任务1副本[2629:60b] -[YouVC loadView]
2014-08-13 15:16:45.925 任务1副本[2629:60b] -[YouVC viewDidLoad]
2014-08-13 15:16:45.925 任务1副本[2629:60b] -[YouVC loadView]
2014-08-13 15:16:45.926 任务1副本[2629:60b] -[YouVC viewDidLoad]
2014-08-13 15:16:45.927 任务1副本[2629:60b] -[YouVC loadView]
2014-08-13 15:16:45.927 任务1副本[2629:60b] -[YouVC viewDidLoad]
2014-08-13 15:16:45.928 任务1副本[2629:60b] -[YouVC loadView]
2014-08-13 15:16:45.928 任务1副本[2629:60b] -[YouVC viewDidLoad]
2014-08-13 15:16:45.929 任务1副本[2629:60b] Application windows are expected to have a root view controller at the end of application launch



无解 ,网上搜得所有方案都不对。我把焦点放在了 self.window.rootviewcontroller = ....这句话上了;


我开始想它出错的原因,为什么这些方法调用了好几遍,单步调试发现这些函数不止是在初始化的init时候调用。

我又新建了一个viewcontroller就加了一个背景色,结果能运行,我又试着将师傅给的代码贴上去,结果又出现错误了。

我就开始挨个试是什么出现了问题,最后让我找到了原因出在-(void)loadview这个函数上面;

我又开始查询这个函数,最后从M了个J老师这获得了答案:

原来在每次调用view的时候,如果view为空,则会调用这个函数;他的作用就是来创建一个view,首先加载xib 如果没有xib 则自动创建一个大小320*480 的空白view;

在程序的

-(void)loadview 中加入了[super loadview];后程序能够运行了;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}
-(id)init
{
    if(self = [super init])
    {
        self.view.frame=[[UIScreen mainScreen]bounds];
        self.view.backgroundColor = [UIColor orangeColor];
    }
    return self;
}
- (void)loadView
{
   // [super loadView];
    NSLog(@"%s",__FUNCTION__);

}

- (void)viewDidLoad
{
    [super viewDidLoad];
    NSLog(@"%s",__FUNCTION__);
    // Do any additional setup after loading the view from its nib.
}
- (void)viewWillAppear:(BOOL)animated
{
    NSLog(@"%s",__FUNCTION__);
}
- (void)viewDidAppear:(BOOL)animated
{
    NSLog(@"%s",__FUNCTION__);
}

- (void)layoutSublayersOfLayer:(CALayer *)layer
{
    NSLog(@"%s",__FUNCTION__);
    
}
- (void)viewWillLayoutSubviews
{
    NSLog(@"%s",__FUNCTION__);
}

 

运行结果:

2014-08-13 15:35:54.783 任务1副本[2660:60b] -[YouVC loadView]
2014-08-13 15:35:54.783 任务1副本[2660:60b] -[YouVC viewDidLoad]
2014-08-13 15:35:54.784 任务1副本[2660:60b] -[YouVC viewWillAppear:]
2014-08-13 15:35:54.785 任务1副本[2660:60b] -[YouVC viewWillLayoutSubviews]
2014-08-13 15:35:54.835 任务1副本[2660:60b] -[YouVC viewDidAppear:]

这个就是 创建view的时候,相关view创建函数的运行次序:

M了个J老师的博客 http://www.cnblogs.com/mjios/archive/2013/02/26/2933667.html里面详细讲述了这几个函数;

ios view的创建过程,布布扣,bubuko.com

ios view的创建过程

原文:http://blog.csdn.net/u010123208/article/details/38536929

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!