1.0 multi-Controller Management (Appendix: Demo), 1.0 demo
Controller:
- An iOS app is rarely composed of only one controller, unless the app is extremely simple
- When the app has multiple controllers, we need to manage these controllers.
|
- When there are multiple views, you can use a large view to manage one or more small views. The same is true for controllers. Use one controller to manage multiple other controllers.
- For example, controller A manages three controllers, namely, B, C, and D. Controller A is called the "parent controller" of controller B, C, and D ", controllers B, C, and D are called "subcontrollers" of Controller"
|
Create UIViewController:
1. directly create code (alloc + init) |
2. Create a file using the Xib File |
3. Create on storyboard |
1.1.1 create a controller-alloc + init Step: AppDelegate. m
1 # import "AppDelegate. h "2 # import" TDViewController. h "3 4 @ interfaceAppDelegate () 5 @ end 6 7 @ implementation AppDelegate 8 9 // this method is called when the application is started, the application is killed by the operating system. Click the icon again to call this method! 10-(BOOL) application :( UIApplication *) application didfinishlaunchingwitexceptions :( NSDictionary *) launchOptions {11 12 // 1. instantiate UIWindow (Special UIView) 13 self. window = [[ui1_walloc] initWithFrame: [UIScreenmainScreen]. bounds]; 14 15 // 2. alloc init method (instantiated View Controller) 16 TDViewController * vc = [[TDViewControlleralloc] init]; 17 18 self. window. rootViewController = vc; // set the root controller (add the View Controller to the Root View of the window) 19 [self. WindowmakeKeyAndVisible]; // change the window to the main window and 20 21 returnYES; 22} 23 ...............
Create a controller: TDViewController, inherited from UIViewController TDViewController. m
1 # import "TDViewController. h "2 3 @ interfaceTDViewController () 4 @ end 5 6 @ implementation TDViewController 7 8-(void) viewDidLoad {9 [superviewDidLoad]; 10 // Do any additional setup after loading the view.11 NSLog (@ "you loaded TDViewController"); 12} 13 ...............
Print Output:
18:44:11. 706 UIViewController (alloc + init) [2653: 418332] What you load is TDViewController |
1.1.2 create a controller-Xib Error:
Error: '-[UIViewController _ loadViewFromNibNamed: bundle:] was unable to load a nib named "TDView"' solution: drag a view |
Error: '-[UIViewController _ loadViewFromNibNamed: bundle:] loaded the "TDView" nib but the view outlet was not set. 'solution: On the File's Ower of the xib, bind the class to the TDViewController, right-click the View, and connect |
Steps: Create an TDView. xib AppDelegate. m
1 # import "AppDelegate. h "2 # import" TDViewController. h "3 4 @ interfaceAppDelegate () 5 @ end 6 7 @ implementation AppDelegate 8 9 // this method is called when the application is started, the application is killed by the operating system. Click the icon again to call this method! 10-(BOOL) application :( UIApplication *) application11 didfinishlaunchingwitexceptions :( NSDictionary *) launchOptions {12 13 // 1. instantiate UIWindow (Special UIView) 14 self. window = [[ui1_walloc] initWithFrame: [UIScreenmainScreen]. bounds]; 15 16 // 2. load the specified xib file 17 TDViewController * vc = 18 [[TDViewControlleralloc] initWithNibName: @ "TDView" bundle: nil]; 19 20 self. window. rootViewController = vc; // set the root controller (add the View Controller to wi On the Root View of the ndow window) 21 [self. windowmakeKeyAndVisible]; // you can change the window to the main window and see 22 23 returnYES; 24} 25 ...............
Create a controller: TDViewController, inherited from UIViewController TDViewController. m
1 # import "TDViewController. h "2 3 @ interfaceTDViewController () 4 @ end 5 6 @ implementation TDViewController 7 8-(void) viewDidLoad {9 [superviewDidLoad]; 10 // Do any additional setup after loading the view.11 NSLog (@ "you loaded TDViewController"); 12} 13 ...............
Print Output:
19:08:43. 400 UIViewController (Xib) [3121: 549327] What you load is TDViewController |
1.1.3 create a controller-sb Steps: Create a TD. storyboard AppDelegate. m
1 # import "AppDelegate. h "2 # import" TDViewController. h "3 4 @ interfaceAppDelegate () 5 @ end 6 7 @ implementation AppDelegate 8 9 // this method is called when the application is started, the application is killed by the operating system. Click the icon again to call this method! 10-(BOOL) application :( UIApplication *) application11 didfinishlaunchingwitexceptions :( NSDictionary *) launchOptions {12 13 // 1. instantiate UIWindow (Special UIView) 14 self. window = [[ui1_walloc] initWithFrame: [UIScreenmainScreen]. bounds]; 15 16 // 2. load the sb file first (Class in sb should point to the Class Name of the Controller to be created) (nil passed by bundle is main bundle by default) 17 UIStoryboard * sb = [UIStoryboardstoryboardWithName: @ "TD" bundle: nil]; 18 19 // 3. then initialize the control in the storyboard. Device 20 // Method 1: Initialize the "initial controller" (Controller indicated by the arrow in sb) <create a controller view by loading the sb file, that is, View Controller> 21 TDViewController * vc = [sb instantiateInitialViewController]; 22 23 // Method 2: Initialize the corresponding controller through an identifier (Storyboard ID needs to be set on the controller of sb) 24 TDViewController * vc = [sb instantiateViewControllerWithIdentifier: @ "TD"]; 25 26 self. window. rootViewController = vc; // set the root controller (add the View Controller to the Root View of the window) 27 [self. windowmakeKeyAndVisible]; // Changes the window to the main window and 28 29 returnYES is visible; 30} 31 ...............
Create a controller: TDViewController, inherited from UIViewController TDViewController. m
1 # import "TDViewController. h "2 3 @ interfaceTDViewController () 4 @ end 5 6 @ implementation TDViewController 7 8-(void) viewDidLoad {9 [superviewDidLoad]; 10 // Do any additional setup after loading the view.11 NSLog (@ "you loaded TDViewController"); 12} 13 ...............
Print Output:
19:19:53. 029 UIViewController (storyboard) [3381: 616265] What you load is TDViewController |
Download Demo: Https://yunpan.cn/crcML8JCvLjsV (extract code: 1fdc) if you think this article is helpful to you, please click the right lower "recommended", ^ _ ^
Author: Loto)
Source: http://www.cnblogs.com/shorfng/
The copyright of this article is shared by the author and the blog. You are welcome to repost this article, but you must keep this statement without the author's consent and provide a connection to the original article on the article page.
If you have any questions, please send an e-mail to shorfng@126.com to contact me.
By: Loto)