[Basic iOS control and basic ios Control
A. Create A controllerCommon methods for creating controllers are as follows:
Create on storyboard
Create directly
1 ViewController *vc = [[ViewController alloc] init];
After the class is set for xib, when the file name of xib is the same as that of the controller class, the xib file specified by the controller in xib will be loaded by default to create
1 ViewController *vc = [[ViewController alloc] initWithNibName:@"ViewController" bundle:nil];
1. Create an Empty Application (without storyboard) from storyboard (2) create a window and add it to screen
1-(BOOL) application :( UIApplication *) application didfinishlaunchingwitexceptions :( NSDictionary *) launchOptions {2 // manually add window to screen3 self. window = [[UIWindow alloc] init]; 4 self. window. frame = [[UIScreen mainScreen] bounds]; 5 self. window. backgroundColor = [UIColor grayColor]; 6 [self. window makeKeyAndVisible]; 7 return YES; 8}
(3) create a storyboard and drag it into a controller (4) to retrieve the storyboard (in fact, it is equivalent to xib)
1 // 2. Obtain stroyboard2 UIStoryboard * sb = [UIStoryboard storyboardWithName: @ "mysb" bundle: nil];
(5) You can set the controller on the storyboard to rootViewController in two ways to obtain the controllera on the storyboard. directly use the ingress controller. The background color of this view is that the class of ViewController in the olive green storyboard is the custom controller.
1 // 3.1 directly use InitialViewController2 self. window. rootViewController = [sb instantiateInitialViewController];
B. Use the ID to set the class of ViewController and drag a ViewController to set the background color of the view to yellow and the ID to "vc2"
1 // 4. Use the ID to get the controller and set rootViewController2 self. window. rootViewController = [sb instantiateViewControllerWithIdentifier: @ "vc2"];
Completed loading process:
1-(BOOL) application :( UIApplication *) application didfinishlaunchingwitexceptions :( NSDictionary *) launchOptions {2 // 1. manually add window to screen 3 self. window = [[UIWindow alloc] init]; 4 self. window. frame = [[UIScreen mainScreen] bounds]; 5 self. window. backgroundColor = [UIColor grayColor]; 6 [self. window makeKeyAndVisible]; 7 8 // 2. obtain stroyboard 9 UIStoryboard * sb = [UIStoryboard storyboardWithName: @ "mysb" bundle: nil]; 10 11 // 3. extract controller12 from storyboar // 3.1 directly use InitialViewController13 ViewController * controller = [sb instantiateInitialViewController]; 14 15 // 3.2 use ID16 ViewController2 * controller2 = [sb restart: @ "vc2"]; 17 18 // 4. set rootViewController19 self. window. rootViewController = controller2; 20 21 return YES; 22}
Summary:1. when a Single View Application is created, the project will bring a storyboard. In fact, the file of Main storyboard is set in the above case, and storyboard 2 is automatically loaded. different controller classes are responsible for operations on different interfaces. directly create (not detailed) 3. this method is used when the specified xib file is created without a storyboard (1) create a controller (2) create an xib (3) drag two views into the xib file and set some feature identifiers, convenient comparison (4) set one view as the controller's viewa. change the class of File's Owner to custom controller B. set the view (5) of the controller to load the controller from xib and display the view to the window.
1-(BOOL) application :( UIApplication *) application didfinishlaunchingwitexceptions :( NSDictionary *) launchOptions {2 // 1. manually add window to screen 3 self. window = [[UIWindow alloc] init]; 4 self. window. frame = [[UIScreen mainScreen] bounds]; 5 self. window. backgroundColor = [UIColor grayColor]; 6 [self. window makeKeyAndVisible]; 7 8 // load the controller from xib and set rootViewController 9 self. window. rootViewController = [[XibViewController alloc] initWithNibName: @ "myx" bundle: nil]; 10 11 return YES; 12}
Summary:1. storyboard :( here ViewController2 is used as rootViewController) xib :( view1 is used as the display view)
B. Create a view for the controller.There are multiple ways to create a view for the Controller (create by priority, only use the highest priority)
- LoadView code (controller implements the loadView method)
- Storyboard description
- Xib
1. use loadView (1) to create a controller, storyboard, xib (2) Configure storyboard and xib class as custom controller (3) add a clear flag to the views of storyboard and xib (4) Implement loadView (
When the controller's view is empty, loadView is called.)
1 // load the view, which is delayed loading. Call 2-(void) loadView {3 NSLog (@ "loadView... "); 4 self. view = [[UIView alloc] init]; 5 self. view. frame = [[UIScreen mainScreen] bounds]; 6 UILabel * label = [[UILabel alloc] init]; 7 label. frame = CGRectMake (40, 40,100,100); 8 label. text = @ "loadView"; 9 [self. view addSubview: label]; 10}
(5) Configure controller in delegate to window a. configure the controller of storyboard as rootViewController
1-(BOOL) application :( UIApplication *) application didfinishlaunchingwitexceptions :( NSDictionary *) launchOptions {2 // configure window 3 self. window = [[UIWindow alloc] init]; 4 self. window. frame = [[UIScreen mainScreen] bounds]; 5 self. window. backgroundColor = [UIColor grayColor]; 6 7 // configure the controller in the storyboard as rootViewController 8 self. window. rootViewController = [[UIStoryboard storyboardWithName: @ "test" bundle: nil] instantiateInitialViewController]; 9 10 // configure the controller in xib as rootViewController, mainly using controller11/self with loadView. window. rootViewController = [[ViewController alloc] initWithNibName: @ "ViewController" bundle: nil]; 12 13 // display window14 [self. window makeKeyAndVisible]; 15 return YES; 16} 17
B. Similarly, using the controller in xib as the rootViewController will not work if loadView exists.
1 // configure the controller in xib as rootViewController, mainly using controller2 self. window. rootViewController with loadView = [[ViewController alloc] initWithNibName: @ "ViewController" bundle: nil];
Summary:When configuring rootViewController, if the loadView method is implemented in the configured controller, the view in storyboard or xib is overwritten.