"iOS Base Controls-14" Controller && Controller View

Source: Internet
Author: User

A. Creation of the ControllerThere are several common ways to create controllers:
Create with Storyboard

Directly create
1 Viewcontroller *VC = [[Viewcontroller alloc] init];
Xib after setting the class, when the Xib file name is the same as the controller class name, the controller specified xib file in Xib is loaded by default in this method to create
1 Viewcontroller *VC = [[Viewcontroller alloc] initwithnibname:@ "Viewcontroller" bundle:nil];
 1. Create from Storyboard (1) Create an empty application (without storyboard) (2) Create window and add to screen
1-(BOOL) Application: (UIApplication *) application didfinishlaunchingwithoptions: (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, drag into a controller (4) to remove storyboard (in fact, is equivalent to xib)
1     //2. Get Stroyboard2     uistoryboard *sb = [Uistoryboard storyboardwithname:@ "MYSB" bundle:nil];
(5) Set Storyboard controller for Rootviewcontroller There are two ways to get Controllera on storyboard. Directly using the ingress controller, the background color of this view is olive green Set the class of Viewcontroller in storyboard to be a custom controller
1     //3.1 Direct use InitialViewController2     Self.window.rootViewController = [SB Instantiateinitialviewcontroller] ;
B. Use the ID to set the Viewcontroller class and drag into a viewcontroller, set the view background color is yellow, the setting ID is "VC2"
1     //4. Use ID to get controller, set rootViewController2     self.window.rootViewController = [SB instantiateviewcontrollerwithidentifier:@ "VC2"];
Completion of the loading process:
1-(BOOL) Application: (UIApplication *) application didfinishlaunchingwithoptions: (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. Obtained Stroyboard 9     uistoryboard *SB = [Uistoryboard storyboardwithname:@ "MYSB" bundle:nil];10    one     //3. Remove Storyboar in Controller12     //3.1 directly using InitialViewController13     viewcontroller *controller = [SB INSTANTIATEINITIALVIEWCONTROLLER];14     //3.2 using ID16     ViewController2 *controller2 = [SB instantiateviewcontrollerwithidentifier:@ "VC2"];17     //4. Setting RootViewController19     Self.window.rootViewController = controller2;20     return yes;22}
Summary:1. Create a single View application, the project will bring a storyboard, in fact, do the above things set the main storyboard file, will automatically load storyboard 2. Different controller classes are responsible for the operation of different interfaces 2. Create directly (not detailed) 3. Specify that the Xib file is created using this method (1) to create a controller (2) to create a xib (3) Drag in the Xib two view, set some feature identification, convenient contrast (4) Set one of the view for the controller Viewa. Change the class of File ' s Owner to a custom controller B. Set the view of the controller (5) Load the controller from Xib and display the view to the window
1-(BOOL) Application: (UIApplication *) application didfinishlaunchingwithoptions: (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 controller from Xib, set Rootviewcontroller 9     self.window.rootViewController = [[Xibviewcontroller alloc] initwithnibname:@ " Myx "bundle:nil];10     return yes;12}
Summary:1.storyboard: (use ViewController2 here for Rootviewcontroller) Xib: (Use View1 as the view shown) B. Creating a view for a controllerThere are several ways to create a view of a controller (created by priority, using only the preferred method)
    • Loadview Code (Controller implementation Loadview method)
    • Storyboard description
    • Xib

Official documentation for the latest version:

1. Create a controller, storyboard, xib through Loadview (1) (2) Configure the class for the storyboard and xib for the custom controller (3) to storyboard and xib view plus the obvious flag (4) in the Controller class to implement Loadview ( when the controller's view is empty, it calls Loadview .
1//Load view, this is a lazy load when you need to use view while view is empty call 2-(void) Loadview {3     NSLog (@ "Loadview ..."), 4     self.view = [[UIView All OC] 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 to window on delegate A. Controller for configuration storyboard is Rootviewcontroller
1-(BOOL) Application: (UIApplication *) application didfinishlaunchingwithoptions: (Nsdictionary *) launchOptions {2     //config window 3     self.window = [[UIWindow alloc] init]; 4     self.window.frame = [[UIScreen mainscreen] bounds]; 5< C3/>self.window.backgroundcolor = [Uicolor Graycolor]; 6     7     //Configure the Controller in storyboard to Rootviewcontroller 8     self.window.rootViewController = [[Uistoryboard storyboardwithname:@ "Test" bundle:nil] instantiateinitialviewcontroller]; 9      //Configure the controller in the Xib as Rootviewcontroller, mainly using CONTROLLER11 with Loadview//    Self.window.rootViewController = [[Viewcontroller alloc] initwithnibname:@ "Viewcontroller" bundle:nil];12    13     //Display WINDOW14     [Self.window makekeyandvisible];15     return yes;16}17  
Will find no effect B. Similarly, using the controller in Xib as Rootviewcontroller, as long as Loadview exists, does not work
1      //Configuration controller in Xib is 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

"iOS Base Controls-14" Controller && Controller View

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.