IOS, iOS 8
The previous content is supplemented and described.
Another experiment is required, so Test1ViewController and test1VC instance are used. TestViewController and testViewController are used before. This should not affect reading.
Problem
First read the code and create a subclass TestViewController (including the nib file) of UIViewController ):
The imageViewCourse and lbCourse attributes are added using the IBOutlet method of nib. For example:
| Create Test1ViewController |
Add IBOutlet |
|
|
Use the following code to redirect between uiviewcontrollers:
TestViewController *testViewController = [[TestViewController alloc] initWithNibName:@"TestViewController" bundle:nil];testViewController.imageViewCourse.image = image;testViewController.lbCourse.text = course;[self presentViewController:testViewController animated:YES completion:nil];
We use nib to load a TestViewController and assign values to its attributes, and then redirect. The problem is that after initWithNibName is executed, testViewController. imageViewCourse and testViewController. lbCourse are both nil, which means that after the jump to TestViewController, there is no content in imageViewCourse and lbCourse. For example:
| IBOutlet content not updated |
Breakpoint debugging |
|
|
Solution
Use the loadView method to trigger the loading of UIView in 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 is:
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];
The result is as follows:
| IBOutlet update content |
Breakpoint debugging |
|
|
The code above is not explained too much. The three methods are actually the same.
Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.