IOS enables two-way transfer of data between two controllers _ios

Source: Internet
Author: User
Tags uikit

This article for everyone to share the iOS controller data two-way transmission, for your reference, the specific content as follows

First, there are two controllers, controller A, controller B, respectively.
a->b: The data is transmitted by controller A to controller B, which is called the data cis, and the data is transmitted by Controller B to controller A, which is called the reverse transmission.
Shun Chuan: generally by creating a target controller object, the data assigned to the object's members to complete;
inverse: typically used by proxies, where controller A is the agent for Controller B (Controller A listens to Controller B, controller B notifies controller a).
Here is the blogger wrote a simple implementation of two control between the two-way transmission of data between the app demo:
1, this is the interface design:

FirstViewController.h

#import <UIKit/UIKit.h>

@interface firstviewcontroller:uiviewcontroller

@end

Firstviewcontroller.m

#import "FirstViewController.h" #import "SecondViewController.h" @interface Firstviewcontroller () < Secondviewcontrollerdelegate>/** is used to write data, and finally the data is passed to the second interface/@property (weak, nonatomic) Iboutlet Uitextfield *
First2second;

/** is used to display the data that is passed when the second interface returns * * * @property (weak, nonatomic) Iboutlet Uitextfield *displaywithsecond;

@end @implementation Firstviewcontroller-(void) viewdidload {[Super viewdidload]; This method is automatically called when #pragma mark-navigation//Click the Pass button-(void) Prepareforsegue: (Uistoryboardsegue *) Segue Sender: (ID) Sender {S
  Econdviewcontroller *VC = (Secondviewcontroller *) Segue.destinationviewcontroller;
  if (Self.first2Second.text.length > 0) {//Pass the data in the interface to the second interface vc.name = Self.first2Second.text;
//Set Agent vc.delegate = Self for the current controller as the Secondviewcontroller controller; #pragma mark-Implements the Protocol method in Secondviewcontrollerdelegate-(void) Secondviewcontrollerdiddit: (Secondviewcontroller *) Viewcontroller andname: (NSString *) name {//return the data in the second interface to the first interface (this interface) Self.displaywithsecoNd.text = name;

 } @end

SecondViewController.h

#import <UIKit/UIKit.h>
@class secondviewcontroller;

@protocol secondviewcontrollerdelegate <NSObject>

/** secondviewcontrollerdelegate protocol method
-(void) Secondviewcontrollerdiddit: (Secondviewcontroller *) Viewcontroller andname: (NSString *) name;

@end

@interface secondviewcontroller:uiviewcontroller

@property (nonatomic,strong) nsstring *name;
@property (Nonatomic,weak) id<secondviewcontrollerdelegate> delegate;

@end



Secondviewcontroller.m

 #import "SecondViewController.h" @interface Secondviewcontroller ()/** is used to write data, and finally returns the data to
The first interface */@property (weak, nonatomic) Iboutlet Uitextfield *second2first;
/** is used to display the data from the first interface * * * @property (weak, nonatomic) Iboutlet Uitextfield *displaywithfirst;

/** Click this button, the second controller will pop the stack, the interface will return to the first interface * * * (ibaction) Second2first: (UIButton *) sender;

  @end @implementation Secondviewcontroller-(void) viewdidload {[Super viewdidload];

Displays the data information passed over the first interface self.displayWithFirst.text = Self.name; //Click the button, the data will be returned to the first interface display-(ibaction) Second2first: (UIButton *) Sender {if (Self.second2First.text.length > 0) {/ /If there is a controller that implements the Protocol method, the data is passed to the controller if ([Self.delegate respondstoselector: @selector (secondviewcontrollerdiddit:andname:)])
    {[Self.delegate secondviewcontrollerdiddit:self andName:self.second2First.text];
} [Self.navigationcontroller Popviewcontrolleranimated:yes]; } @end 

The above is the entire content of this article, I hope to give you a reference, but also hope that we support the cloud-dwelling community.

Related Article

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.