IOS growth path-pass value on two uiview interfaces-delegate

Source: Internet
Author: User

First, define two interfaces ()

Interface 1:

Interface 2:

Implemented through Delegate: (the first class delegates some of its events to the ViewDelegate class to complete, and the second class operates on this proxy, that is, it assigns it a value, in this way, the first class can obtain the value through the proxy)

First, define a proxy class:

Define your own name, which defines a method for passing values

@ Protocol ViewDelegate-(void) passValue :( NSString *) value; // Method for passing values @ end

Usage:

// In the first Interface Class: # import "ViewDelegate. h "// import the prepared proxy class @ interface ViewController: UIViewController <ViewDelegate> // In. in the m file, write the following redirection method:-(void) Next {NextViewController * next = [[NextViewController alloc] initWithNibName: @ "NextViewController" bundle: [NSBundle mainBundle]; // initialize next interface. delegate = self; // it is most important to pass the self in this class to the delegate/* of the NextViewController class, delegate is the NSObject <ViewDelegate> * delegate; @ property (nonatomic, assign) NSObject <ViewDelegate> * delegate; defined by NectViewController. the delegate = self; statement is like initializing the delegate in the second class. It also makes the two classes connected through the proxy class, in the second class, you can use the delegate proxy class object to call its own method for value assignment (when the previous page is returned)-(void) back // click the back method {[delegate passValue: self. nameText. text]; // call the proxy method [self dismissModalViewControllerAnimated: YES] through delegate, it is equivalent to the value stored in the parameters of the passValue method, and then the value passed from the second class can be obtained by implementing this method in the first class.-(void) passValue :( NSString *) value {self. nameLabel. text = value; // display the value in the label.} */[self presentModalViewController: next animated: YES];}

Source code: Click to enter the demo Download Page

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.