Transferring data between IOS Viewcontroller

Source: Internet
Author: User

The problem of translating from StackOverflow

Forward Passing Data:

For example, you have Viewcontrollera and Viewcontrollerb, passing a bool variable from Viewcontrollera to Viewcontrollerb:

(1) Create an attribute in ViewControllerB.h

@property (nonatomic, assign) BOOL *issomethingenabled;

(2) Introduction of header file in Viewcontrollera

#import "ViewControllerB.h"

When you click Didselectrowatindex or ibaction, you can set the properties of the Viewcontrollerb with the following code

Viewcontrollerb *viewcontrollerb = [[Viewcontrollerb alloc] initwithnib:@ "Viewcontrollerb" bundle:nil]; viewcontrollerb.issomethingenabled = YES; [Self pushviewcontroller:viewcontrollerb animated:yes];

Passing data in front of the segue:

Pass the data in the following ways:

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

(1) Create an attribute in ViewControllerB.h

@property (nonatomic, assign) BOOL *issomethingenabled;

(2) Introduction of header file in Viewcontrollera

#import "ViewControllerB.h"

(3) The establishment of a segue from Viewcontrollera to Viewcontrollerb, may be named Showdetailsegue

(4) Implementation method:

-(void) Prepareforsegue: (Uistoryboardsegue *) Segue Sender: (ID) sender{    if ([Segue.identifier isequaltostring:@] Showdetailsegue "]) {        Viewcontrollerb *controller = (Viewcontrollerb *) Segue.destinationviewcontroller;        controller.issomethingenabled = YES;}    }

If your view is nested inside the navigation controller,

-(void) Prepareforsegue: (Uistoryboardsegue *) Segue Sender: (ID) sender{    if ([Segue.identifier isequaltostring:@] Showdetailsegue "]) {        Uinavigationcontroller *navcontroller = (Uinavigationcontroller *) Segue.destinationviewcontroller;        Viewcontrollerb *controller = (Viewcontrollerb *) Navcontroller.topviewcontroller;        controller.issomethingenabled = YES;}    }

To pass data back:

Viewcontrollera inside click some buttons, jump to Viewcontrollerb, the inside of the event after the completion of the execution, but also jump back to Viewcontrollera, and some data to the Viewcontrollera. Proxy mode is required here.

(1) in ViewControllerB.h, #import下, declare an agreement on the @interface

@protocol viewcontrollerbdelegate <nsobject>-(void) Additemviewcontroller: (Viewcontrollerb *) controller Didfinishenteringitem: (NSString *) item; @end

(2) Setting a delegate property in ViewControllerB.h

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

(3) In Viewcontrollerb, when we quit this viewcontroller,

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

(4) in ViewControllerA.h, introduce the header file and implement the Protocol

#import "ViewControllerB.h" @interface Viewcontrollera:uiviewcontroller <ViewControllerBDelegate>

(5) in VIEWCONTROLLERA.M,

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

(6) Set up Agent

Viewcontrollerb *viewcontrollerb = [[Viewcontrollerb alloc] initwithnib:@ "Viewcontrollerb" bundle:nil]; Viewcontrollerb.delegate = self[[self Navigationcontroller] Pushviewcontroller:viewcontrollerb Animated:YES];

 

  

  

Transferring data between IOS Viewcontroller

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.