Ios view creation process
The master sent me a bunch of view creation functions to let me know the view creation process.
- (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{}
I didn't think much about this function, so I created a single View application, created a viewcontroller, copied the master's function, and wrote it in the AppDelegeteD. m file.
- (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;}
Then run ...... It's cool... An error occurred;
The black screen is started, and the following error is reported: Application windows are expected to have a root view controller at the end of application launch
15:16:45. 924 Task 1 copy [2629: 60b]-[YouVC loadView] 15:16:45. 925 Task 1 copy [2629: 60b]-[YouVC viewDidLoad] 15:16:45. 925 Task 1 copy [2629: 60b]-[YouVC loadView] 15:16:45. 926 Task 1 copy [2629: 60b]-[YouVC viewDidLoad] 15:16:45. 927 Task 1 copy [2629: 60b]-[YouVC loadView] 15:16:45. 927 Task 1 copy [2629: 60b]-[YouVC viewDidLoad] 15:16:45. 928 Task 1 copy [2629: 60b]-[YouVC loadView] 15:16:45. 928 Task 1 copy [2629: 60b]-[YouVC viewDidLoad] 15:16:45. 929 Task 1 copy [2629: 60b] Application windows are expected to have a root view controller at the end of application launch
No solution. All solutions searched online are incorrect. I focused on self. window. rootviewcontroller =;
I started to think about the cause of the Error. Why have these methods been called several times? One-Step debugging found that these functions are not only called during init initialization.
I created a new viewcontroller and added a background color. The result was running. I tried to paste the code from the master and the result was wrong again.
I started to try out the problem, and finally found out the reason for the-(void) loadview function;
I started to query this function again, and finally got the answer from M's J INSTRUCTOR:
It turns out that when you call the view, if the view is empty, this function is called. Its role is to create a view, first, load the xib file. If no xib file exists, a blank view with a size of 320*480 is automatically created;
In
-(Void) added [super loadview] To loadview. Then the program can run;
- (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__);}
Running result:
15:35:54. 783 Task 1 copy [2660: 60b]-[YouVC loadView] 15:35:54. 783 Task 1 copy [2660: 60b]-[YouVC viewDidLoad] 15:35:54. 784 Task 1 copy [2660: 60b]-[YouVC viewWillAppear:] 15:35:54. 785 Task 1 copy [2660: 60b]-[YouVC viewWillLayoutSubviews] 15:35:54. 835 Task 1 copy [2660: 60b]-[YouVC viewDidAppear:]
This is the running sequence of the related view creation functions when the view is created:
M has a blog post from instructor J;