UIStoryboard Load Controller
1. Load the controller pointing at the arrow
self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds]; //Create UIStoryboard object UIStoryboard *sb = [UIStoryboard storyboardWithName:@"Main" bundle:nil]; //Load the controller for the specified arrow UIViewController *rootVC = [sb instantiateInitialViewController]; self.window.rootViewController = rootVC; [self.window makeKeyAndVisible];
2. Load a controller with an Identity
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds]; //Create UIStoryboard object UIStoryboard *sb = [UIStoryboard storyboardWithName:@"Main" bundle:nil]; //Load the controller for the specified arrow UIViewController *rootVC = [sb instantiateViewControllerWithIdentifier:@"testSB"]; self.window.rootViewController = rootVC; [self.window makeKeyAndVisible]; return YES; }
Jump between ###3.Storyboard
-
Jump between controllers, you can see from the diagram that when segue is selected, it is from the source controller to the target controller
-
Bind a jump label to each segue, blue control is oneVC, pink controller is twoVC
-
When I click the Jump button, I have to choose which controller to jump to
//Jump Controller - (IBAction)handleSelectController:(UIButton *)sender { if ([self.isSelectStr isEqualToString:@"oneVC"]) { [self performSegueWithIdentifier:@"oneVC" sender:nil]; }else { [self performSegueWithIdentifier:@"twoVC" sender:nil]; } }
- performSegueWithIdentifier: sender: The following method is called inside the method
//Call this method to pass a value - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{ // segue.sourceViewController h gets the source controller //Target Controller CCOneVC *oneVC = segue.destinationViewController; oneVC.targetStr = @"This is a value-passing property"; }
- So, generally in a segue jump, to pass a value, call this method, according to the identity, to different controllers, pass a value
XIB Load Controller
Create CCXIBVC file
Create the XIB file, bind the controller, and go offline to View as follows:
- Load xib file controllers with different names
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds]; CCXibVC *xibVC = [[CCXibVC alloc] initWithNibName:@"xib" bundle:nil]; self.window.rootViewController = xibVC; [self.window makeKeyWindow]; return YES; }
- Load a xib file controller with the same name
-
init load
Loading principle of xib
- Load xib files directly from initWithNibName:bundle, regardless of whether the xib file is the same as the class name
- Load through init
1.init underlying calls initWithNibName:bundle:method
2. When initWithNibName:bundle:initializes the controller and nibName is nil, the system will do the following
2.1, find out if there is Xib with the same name as the controller class name but without Controller, and load it if there is (CCRootView.xib)
2.2 Find out if there is a Xib with the same name as the controller class name and load it if there is one (CCRootViewController.xib)
2.3 If neither of the above is found, a blank View will be created
###Controller View life cycle
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds]; CCXibVC *xibVC = [[CCXibVC alloc] initWithNibName:@"xib" bundle:nil]; self.window.rootViewController = xibVC; [self.window makeKeyWindow]; return YES; return YES; }
From the code above, we can see that the controller was created successfully
What should I do once the controller has been successfully created?
After the controller is created successfully, the system creates a view and calls - (void)loadView; method
- (void)loadView; the method is actually a lazy load, after creating the view, the viewDidLoad method is called
- (UIView *)view{ if (!_view) { _view = [[UIView alloc] initWithFrame:[UIScreen mainScreen].bounds]; [self viewDidLoad]; } return _view; }
1 - Called when the viewDidLoad controller View has finished loading
- (void)viewDidLoad { [super viewDidLoad]; }
2-Called when the Controller View is about to display, the following method
- (void)viewWillAppear:(BOOL)animated { [super viewWillAppear:animated]; }
3-Called when the View of the controller is displayed
- (void)viewDidAppear:(BOOL)animated { [super viewDidAppear:animated]; }
4-Called when the Controller View is about to disappear
- (void)viewWillDisappear:(BOOL)animated { [super viewWillDisappear:animated]; }
5-Called when the Controller View has disappeared
- (void)viewDidDisappear:(BOOL)animated{ [super viewDidAppear:animated]; }
6-Called when the controller's View is about to lay out the child controls and when the controller's View lays out the child controls. These two methods are called more frequently whenever a child control is added ()
//Called when the controller's View is about to lay out child controls - (void)viewWillLayoutSubviews{ [super viewWillLayoutSubviews]; } //Called when the controller View layout subcontrol is complete - (void)viewDidLayoutSubviews{ [super viewDidLayoutSubviews]; }
7-When memory is warned
- (void)didReceiveMemoryWarning{ [super didReceiveMemoryWarning]; }
8-These two methods are called using MRC (Manual Memory Management), but ARC (System Helps Manage Memory) is now obsolete.
//view is about to be destroyed - (void)viewWillUnload NS_DEPRECATED_IOS(5_0,6_0) __TVOS_PROHIBITED; //view has been destroyed - (void)viewDidUnload NS_DEPRECATED_IOS(3_0,6_0) __TVOS_PROHIBITED;
- Calls to View's life cycle methods