師傅發我一堆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 { }
- (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:]
M了個J老師的博客 http://www.cnblogs.com/mjios/archive/2013/02/26/2933667.html裡面詳細講述了這幾個函數;