Delegate transfer value between iOS view Controllers tutorial

Source: Internet
Author: User

Before on the StackOverflow saw a biography value (Segue and delegate value) of the article, feel speak very clear, will delegate part translated. Interested to have a look.

Original:

Http://stackoverflow.com/questions/5210535/passing-data-between-view-controllers


President

to get fromViewcontrollerbThe value is returned toViewcontrollera, we need to use the agreement(Protocols)and Agents(Delegates).

to implement this process, we need to set Viewcontrollera to be Viewcontrollerb the agent.

This allows viewcontrollerb to send messages to Viewcontrollera, and the same can enable us to return the data.

Viewcontrollera as a Viewcontrollerb Agent must comply with our Viewcontrollerb The protocols defined in (Protocols) , which can tell Viewcontrollera which methods are required to be implemented.


1. Span lang= "ZH-CN" style= "font-size:11pt" in viewcontrollerb.h #import and @interface

@class viewcontrollerb //Important

@protocol Viewcontrollerbdelegate <nsobject ;
-( void ) Additemviewcontroller: (viewcontrollerb *) Controller Didfinishenteringitem: ( Span style= "COLOR: #2B91AF" >nsstring *) item;
@end

Note: (NSString *) Item is the type of data we want to return now, and it can be other types, such as dictionaries, arrays, etc.

2. It is still in the ViewControllerB.h . Set a delegate property at the same time in viewcontroller. M synthesize

@property (nonatomic, weak) ID <viewcontrollerbdelegate>delegate;

I did this in project:

@propertyID<selectpeoplevcdelegate>delegate;

3.in theViewcontrollerb, we send a message to the agent when we are going to pop the view from the navigation controller(The message contains the value we want to pass.)

nsstring *itemtopassback = @"Pass This value, back to Viewcontrollera";
[Self. delegate Additemviewcontroller:self Didfinishenteringitem:itemtopassback];

This is how I ended up in actual project:

-(void) viewdiddisappear: ( BOOL ) Animated

{

[self. Delegate additemviewcontroller:selfdidfinishselectpeople: Datasourcearray];

}

Note:datasourcearray is my data source, in a public variable, in the previous program completed assignment.

4.the above is all to beViewcontrollerbin the operation. Next isViewcontrolleraThe operation.

First we will import the viewcontrollerbin ViewControllerA.h and follow its protocol:

#import "ViewControllerB.h"

@interface viewcontrollera :uiviewcontroller <viewcontrollerbdelegate>

5.in theVIEWCONTROLLERA.MTo implement the Protocol method:

-(void) Additemviewcontroller: (viewcontrollerb *) controller didfinishenteringitem: (nsstring *) Item
{
NSLog (@"This is returned from Viewcontrollerb%@", item);
}

6.Finally, before we willViewcontrollerbbefore pressing into the stack, we need to tellViewcontrollerb,Viewcontrollerait's a proxy. (delegate):

viewcontrollerb *viewcontrollerb = [[viewcontrollerb alloc] initwithnib:@"Viewcontrollerb" Bundle:nil];
Viewcontrollerb. delegate = self
[Self Navigationcontroller] pushviewcontroller:viewcontrollerb Animated:yes];

In actual project I did this:

-(void) Prepareforsegue: ( Uistoryboardsegue *) Segue Sender: (ID) sender

{

uiviewcontroller * viewcontroller = segue. Destinationviewcontroller ;

BAGSELECTPEOPLEVC * Selectpeoplevc = (bagselectpeoplevc *) Viewcontroller;

SELECTPEOPLEVC. Delegate = Self ;

}


Delegate transfer value between iOS view Controllers tutorial

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.