The controller is simply to make the interface jump, similar to the Android intent
1. Create a Controller
1 common ways to create a controller2 3 1) created with storyboard4 5 //directly Create6Njviewcontroller *nj =[[Njviewcontroller alloc]init];7 8 //Specify the Xib file9Njviewcontroller *nj =[[Njviewcontroller alloc]TenInitwithnibname:@"Njviewcontroller"Bundle:nil]; One A //Load the storyboard file first (test is the file name of Stroyboard) -Uistoryboard *storyboard = [Uistoryboard storyboardwithname:@"Test"Bundle:nil]; - the then initialize the controller in the storyboard -Initialization"Initial Controller"(the controller that the Arrow refers to) -Njviewcontroller *nj =[Storyboard Instantiateinitialviewcontroller]; - + initialize the corresponding controller with an identity -Njviewcontroller *nj=[storyboard Instantiateviewcontrollerwithidentifier:@"NJ"];
Mjviewcontroller View Creation Diagram
Loading of the Controller
1 1 The view of the controller is delayed loading and then loaded 2 2 . You can use the Isviewloaded method to determine whether a Uiviewcontroller view has been loaded 3 3. The controller's view is loaded and the Viewdidload method is called
Multi-Controller
1 1an iOS app rarely consists of a single controller, unless the app is extremely simple2 2when there are multiple controllers in the app, we need to manage these controllers3 3when you have multiple view, you can use a large view to manage 1 or more small view4 4Controller is the same, with 1 controllers to manage the other multiple controllers5 6 For example:7Use a controller A to manage 3 controllers B, C, D Controller A is called Controller B, C, D"Parent Controller", controller B, C, and D are called controller A's sub-controllers8 9 to facilitate the management of the Controller, iOS offers 2 more special controllersTen Uinavigationcontroller OneUitabbarcontroller
Uinavigationcontroller
With Uinavigationcontroller, you can easily manage multiple controllers and easily switch between controllers, typically the system's own "settings" app
Uinavigationcontroller Steps to use
1 1. Initialize Uinavigationcontroller2 2. Set the Rootviewcontroller for UIWindow to Uinavigationcontroller3 3Add the corresponding number of sub-controllers by the push method, depending on the situation.4 5 Uinavigationcontroller to save the sub-controller in the form of a stack6@property (nonatomic,copy) Nsarray *viewcontrollers;7@property (Nonatomic,ReadOnly) Nsarray *childviewcontrollers;8 9 use the push method to push a controller into the stackTen-(void) Pushviewcontroller: (Uiviewcontroller *) Viewcontroller animated: (BOOL) animated; One A Use the Pop method to remove the controller - //Remove the controller from the top of the stack --(Uiviewcontroller *) popviewcontrolleranimated: (BOOL) animated; the //back to the specified child controller --(Nsarray *) Poptoviewcontroller: (Uiviewcontroller *) Viewcontroller animated: (BOOL) animated; - //Back to root controller (stack bottom controller) --(Nsarray *) poptorootviewcontrooleranimated: (BOOL) animated;
Modify the contents of the navigation bar
1 the contents of the navigation bar are determined by the Navigationitem property of the stack top controller2 3 Uinavigationitem has the following properties that affect the contents of the navigation bar4 //the Back button in the upper-left corner5@property (nonatomic,retain) Uibarbuttonitem *Backbarbuttonitem;6 //the middle title view7@property (nonatomic, retain) UIView *Titleview;8 //Middle Caption Text9@property (nonatomic,copy) NSString *title;Ten //the view in the upper-left corner One@property (nonatomic,retain) Uibarbuttonitem *Leftbarbuttonitem; A //uibarbuttonitem *rightbarbuttonitem View in the upper right corner -@property (Nonatomic,retain) Uibarbuttonitem *rightbarbuttonitem;
Segue
Each line on the storyboard for interface jumps is a Uistoryboardsegue object (abbreviated segue)
Properties of the Segue
Each of the Segue objects has 3 properties
1 // uniquely identifies 2 readonly) NSString *identifier; 3 // Source Controller 4 @property (nonatomic,readonlyID sourceviewcontroller; 5 // Target Controller 6 @property (nonatomic,readonlyID destinationviewcontroller;
According to Segue's execution (jump) moment, segue can be divided into 2 major types
Auto-type: After clicking on a control (such as a button), automatic execution of Segue, auto-complete interface jump
Manual type: Need to write code to manually perform segue to complete the interface jump
Automatic type:
Manual type:
Performseguewithldentifier:sender:
Use Performseguewithidentifier: Method can execute a segue, complete interface jump
Next study Performseguewithidentifier:sender: The complete execution of the method
[Self performseguewithidentifier:@ "login2contacts" sender:nil];
This self is the source controller.
1. According to identifier to find the corresponding line in the storyboard, create a new Uistoryboardsegue object
Set the Sourceviewcontroller of the Segue object (source Controller)
New and set Segue
2Call Sourceviewcontroller The following method, do some pre-jump preparation and pass in the created Segue object-(void) Prepareforsegue: (Uistoryboardsegue *) Segue Sender: (ID) sender;//This sender was the sender of the Performseguewithidentifier:sender:3. Call the Segude Object-(void) perform; The Sourceviewcontroller method starts to perform the interface jump operation to get the Uinavigationcontroller call Uinavigationcontroller the push method to press the Destinationviewcontroller into the stack , complete the Jump [self performseguewithidentifier:@"login2contacts"Sender@"Jack"];-(void) Perpareforsegue: (Uistoryboardsegue *) Segue Sender: (ID) sender;
Data transfer of the Controller
There are two main cases of data transfer between controllers: pass-through and reverse-transfer
Shun Chuan
1 The direction of the:a-->C2 data transfer direction: a--->c3 Data transfer method: in a Perpareforseque:sender method In accordance with the Segue parameter obtained Destinationviewcontroller, that is, controller C, directly to the Controller C pass data 4 ( To get the data in the Viewdidload method of C, assign a value to the UI control on the interface)
Reverse transmission
1 direction of the controller jump: A-andC2 data transfer direction: c-->a3 data transfer mode: Let a become a C agent, in C call a proxy method, Passing data to a through the parameters of the proxy method
Controller for iOS