007-Multi-Controller Management (data transfer between controllers)

Source: Internet
Author: User
Master • 1. Multiple methods for creating controllers and views • 2. simple use of uinavigationcontroller: Add/Remove sub-controllers • 3. uinavigationbar content settings • 4. segue Data Transfer • 1. Multiple methods for creating controllers and views

Question 1: How to Create a controller?

• Common methods for creating controllers are as follows: 1. Create controllers using storyboard • load the storyboard file first (test is the name of the storyboard file)

Uistoryboard * storyboard = [uistoryboard storyboardwithname: @ "test" Bundle: Nil];

 

• Initialize the controller in the storyboard. initialize the "initial controller" (Controller indicated by the arrow)

Whbviewcontroller * Vc = [storyboard instantiateinitialviewcontroller];

Initialize the corresponding controller through an identifier

Whbviewcontroller * Vc = [storyboard instantiateviewcontrollerwithidentifier: @ "VC"];

2 directly create

Whbviewcontroller * Vc = [[whbviewcontroller alloc] init];

3. Specify the XIB file to create

Whbviewcontroller * Vc = [[whbviewcontroller alloc] initwithnibname: @ "whbviewcontroller" Bundle: Nil];

Question 2: What are the priorities of view creation modes?

 

PS: delayed loading of the controller View

• The Controller's view is delayed loading: loaded when used • you can use the isviewloaded method to determine whether the view of a uiviewcontroller has been loaded • after the Controller's view is loaded, the viewdidload method is called. • 2. simple use of uinavigationcontroller: Add/Remove sub-controllers • uinavigationcontroller use steps ø initialize uinavigationcontroller ø set the rootviewcontroller of uiwindow to uinavigationcontroller. ø depending on the actual situation, add the corresponding number of sub-controllers using the push method • uinavigationcontroller saves the sub-controllers in the form of stacks

@ Property (nonatomic, copy) nsarray * viewcontrollers;

@ Property (nonatomic, readonly) nsarray * childviewcontrollers;

 

• Use the push method to push a controller to the stack

-(Void) pushviewcontroller :( uiviewcontroller *) viewcontroller animated :( bool) animated;

 

• The pop method can be used to remove the Controller ø remove the Controller at the top of the stack

-(Uiviewcontroller *) popviewcontrolleranimated :( bool) animated;

Return to the specified sub-Controller

-(Nsarray *) poptoviewcontroller :( uiviewcontroller *) viewcontroller animated :( bool) animated;

Return to the root controller (stack bottom Controller)

-(Nsarray *) poptorootviewcontrolleranimated :( bool) animated;

• 3. uinavigationbar content settings • the content of the navigation bar is determined by the navigationitem attribute of the stack top controller. • The following attributes of uinavigationitem affect the content of the navigation bar.

@ Property (nonatomic, retain) uibarbuttonitem * backbarbuttonitem;

Ø middle title View

@ Property (nonatomic, retain) uiview * titleview;

Title text in the middle

@ Property (nonatomic, copy) nsstring * title;

Ø view in the upper left corner

@ Property (nonatomic, retain) uibarbuttonitem * leftbarbuttonitem;

Ø uibarbuttonitem * rightbarbuttonitem view in the upper-right corner

@ Property (nonatomic, retain) uibarbuttonitem * rightbarbuttonitem;

 

• 4. Data Transmission of segue

Question 1: What is segue?

• Each line on the storyboard used to jump to the interface is a uistoryboardsegue object, as shown in the following example: Question 2: What are the attributes of segue? • Each segue object has three attributes: Unique Identifier

@ Property (nonatomic, readonly) nsstring * identifier;

Ø source Controller

@ Property (nonatomic, readonly) ID sourceviewcontroller;

Ø Target Controller

@ Property (nonatomic, readonly) ID destinationviewcontroller;

Question 3: What types of segue are available?

• Based on the execution (jump) Time of segue, segue can be divided into two major types: automatic mode: click a control (such as a button), automatic execution of segue, Automatic completion of interface jump, as shown in: press and hold the control key, drag the control line directly to the target controller, and click the "Log on" button, it will automatically jump to the Controller PS on the right: After clicking a control, if you do not want to perform any processing during page Jump and want to jump to the next page, we recommend that you use the "Automatic segue" (manual mode): You need to manually execute the segue by writing code to complete the page Jump, as shown in: press and hold the control key to set an identifier (as shown in) for the source controller to drag the line to the target controller's Manual segger. Question 4: How to manually execute the manual segger in the code to achieve interface jump? • Using javasmseguewithidentifier: You can execute a specific seue to complete the interface jump. Write the following code in the corresponding listening event Method of the source controller to complete the jump between interfaces.
[Self authentication mseguewithidentifier: @ "addcontextsegue" Sender: Nil]; // This self is the source Controller
• Complete mseguewithidentifier: Sender: complete method execution process 1. locate the corresponding line in storyboard Based on identifier and create uistoryboardsegue object 1. set sourceviewcontroller (source Controller) 2 of the segue object "create and set destinationviewcontroller (Target Controller) 2 of the segue object. call the following method of sourceviewcontroller to prepare for the jump and pass in the created segue object.
-(Void) prepareforsegue :( uistoryboardsegue *) segue sender :( ID) sender; // This sender is the sender passed in when mseguewithidentifier: Sender:
3. call the-(void) perform of the segue object to start the interface jump operation. Obtain the uinavigationcontroller in which sourceviewcontroller is located. Call the push method of uinavigationcontroller to push destinationviewcontroller to the stack. Question 5: how to transmit data between controllers? • There are two main data transmission situations between controllers: downstream and reverse data transmission. The redirection direction of the downstream controller is a ------> C Data Transmission Direction: A ------> C data transmission method: Get destinationviewcontroller, that is, controller C, according to The seue parameter in the prepareforseue: Sender: Method of, transmit data directly to Controller C (obtain data in viewdidload method of C and assign the value to the UI control on the Interface) ø redirect of the Inverse Controller: A <------ c Data Transmission Direction: C ------> A data transmission method: let a become the proxy of C, and call a's proxy method in C, pass data to a through the parameters of the proxy method

 

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.