A. Creation of the ControllerThere are several common ways to create controllers:
Create with Storyboard
Directly create
1 viewcontroller *VC = [[Viewcontroller alloc] init];
Xib when the class is set, when the Xib file name is the same as the controller class name, the controller in Xib is loaded by default in this method Specify the Xib file 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 * 2 // add window to screen manually 3 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. Acquisition of Stroyboard 2 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 of Initialviewcontroller 2 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 Rootviewcontroller 2 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 screen3Self.window =[[UIWindow alloc] init];4Self.window.frame =[[UIScreen mainscreen] bounds];5Self.window.backgroundColor =[Uicolor Graycolor];6 [Self.window makekeyandvisible];7 8 //2. Acquisition of Stroyboard9Uistoryboard *SB = [Uistoryboard storyboardwithname:@"MYSB"Bundle:nil];Ten One //3. Remove the controller from the Storyboar A //3.1 Direct use of Initialviewcontroller -Viewcontroller *controller =[SB Instantiateinitialviewcontroller]; - the //3.2 using ID -ViewController2 *controller2 = [SB instantiateviewcontrollerwithidentifier:@"VC2"]; - - //4. Set Rootviewcontroller +Self.window.rootViewController =Controller2; - + returnYES; A}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 screen3Self.window =[[UIWindow alloc] init];4Self.window.frame =[[UIScreen mainscreen] bounds];5Self.window.backgroundColor =[Uicolor Graycolor];6 [Self.window makekeyandvisible];7 8 //load controller from Xib, set Rootviewcontroller9Self.window.rootViewController = [[Xibviewcontroller alloc] Initwithnibname:@"MYX"Bundle:nil];Ten One returnYES; A}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
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, which is deferred loading when you need to use view and view is empty when calling2- (void) Loadview {3NSLog (@"Loadview ...");4Self.view =[[UIView alloc] init];5Self.view.frame =[[UIScreen mainscreen] bounds];6UILabel *label =[[UILabel alloc] init];7Label.frame = CGRectMake ( +, +, -, -);8Label.text =@"Loadview";9 [Self.view Addsubview:label];Ten}(5) Configure Controller to window on delegate A. Controller for configuration storyboard is Rootviewcontroller
1-(BOOL) Application: (UIApplication *) application didfinishlaunchingwithoptions: (Nsdictionary *) launchoptions {2 //Configure Window3Self.window =[[UIWindow alloc] init];4Self.window.frame =[[UIScreen mainscreen] bounds];5Self.window.backgroundColor =[Uicolor Graycolor];6 7 //Configuring the Controller in storyboard to Rootviewcontroller8Self.window.rootViewController = [[Uistoryboard storyboardwithname:@"Test"Bundle:nil] Instantiateinitialviewcontroller];9 Ten //the controller in the configuration xib is Rootviewcontroller, mainly using a controller with Loadview One //Self.window.rootViewController = [[Viewcontroller alloc] initwithnibname:@ "Viewcontroller" bundle:nil]; A - //Show Window - [Self.window makekeyandvisible]; the returnYES; - } - Will find no effect B. Similarly, using the controller in Xib as Rootviewcontroller, as long as Loadview exists, does not work
1 // the controller in the configuration xib is Rootviewcontroller, mainly using a controller with Loadview 2 Self.window.rootViewController = [[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 control 6.11.1]-Controller & Controller View