Delegate transfer value between iOS view Controllers tutorial

Source: Internet
Author: User

Before on the StackOverflow saw a preach value (Segue and delegate value) of the article, feel speak very clear, will delegate part translated a bit, interested can see.

Original:

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


President

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

to implement this process, we need to set viewcontrollera viewcontrollerb viewcontrollerb can send messages to viewcontrollera

viewcontrollera as viewcontrollerb Agent must obey us in viewcontrollerb ( protocols viewcontrollera What are the methods that must 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, or it can be other types, such as dictionaries, arrays, etc.

2. still in ViewControllerB.h , set a delegate property while Viewcontroller. M medium synthesize

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

This is what I did in the 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];

In the actual project I have done this:

-(void) viewdiddisappear: ( BOOL ) Animated

{

[self. Delegate additemviewcontroller:selfdidfinishselectpeople: Datasourcearray];

}

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

4.the above is all that is to beViewcontrollerband the next step is toViewcontrollerathe operation. First, we're going toViewControllerA.hImport inViewcontrollerb, and comply with 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];

This is what I did in the actual project:

-(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.