Problem
Look at the code first, create a new subclass of Uiviewcontroller Testviewcontroller (including the nib file), Imageviewcourse and Lbcourse are its two properties, added by the Iboutlet way of nib.
TestViewController *testViewController = [[TestViewController alloc] initWithNibName:@"TestViewController" bundle:nil];testViewController.imageViewCourse.image = image;testViewController.lbCourse.text = course;[self.navigationController presentViewController:testViewController animated:YES completion:nil];
We use NIB to load a testviewcontroller and assign values to its properties, and then jump. The problem is that after the execution of Initwithnibname, Testviewcontroller.imageviewcourse and Testviewcontroller.lbcourse are nil, It shows that after jumping to testviewcontroller, there is no content in Imageviewcourse and Lbcourse.
Workaround
The
uses the Loadview method to trigger the loading of uiview in the nib.
@property(null_resettable, nonatomic,strong) UIView *view; // The getter first invokes [self loadView] if the view hasn‘t been set yet. Subclasses must call super if they override the setter or getter.- (void)loadView; // This is where subclasses should create their custom view hierarchy if they aren‘t using a nib. Should never be called directly.- (void)loadViewIfNeeded NS_AVAILABLE_IOS(9_0); // Loads the view controller‘s view if it has not already been set.
That
TestViewController *testViewController = [[TestViewController alloc] initWithNibName:@"TestViewController" bundle:nil];//[testViewController view];//[testViewController loadView];[testViewController loadViewIfNeeded];testViewController.imageViewCourse.image = image;testViewController.lbCourse.text = course;[self.navigationController presentViewController:testViewController animated:YES completion:nil];
Code as above, do not explain too much. Three different ways in fact.
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
IOS---loadview usage scenarios in Uiviewcontroller