Several small methods for transferring values between pages in IOS development: ios pages

Source: Internet
Author: User

Several small methods for transferring values between pages in IOS development: ios pages

The most serious way to pass values between pages is to pass values through the proxy. The several small Methods written today do not include pass values through the proxy, because they are self-taught, I don't even know the advantages and disadvantages of these methods. I 'd like to know some of them;

 

First: attribute pass value (this method is not valid in the Story version and is suitable for pure code writing. Why ???)

Premise: for example, there are two view controllers: AViewController and BViewController. They want to pass values from A to B,

-- In the AViewController. m file

(1) Import BViewController

# Import "BViewController. h"

(2) define global variables

For example, we add A UITextfield control in a to pass its value to B.

@property (nonatomic, strong) UITextField *text;

 

 

(3) encoding of the places where values need to be passed

BViewController * Bvc = [[BViewController alloc] initWithNibName: nil bundle: nil]; // value: Bvc. chuanzhi = text. text; // jump to [self. navigationController pushViewController: Bvc animated: YES];

 

 

-- In the BVIewController. h file

When defining the encoding in the previous step, the variable Bvc. chuanzhi must be defined in the. h file;

@property (nonatomic, strong) NSString *chuanzhi;

 

-- In the BViewController. m file

In this way, the passed value can be used everywhere in B;

 

 

Type 2: Pass the value of segue;

Premise: for example, there are two view controllers: AViewController and BViewController. They want to pass values from A to B, and the line between them is Identifier: aTob.

 

-- In the AViewController. m file

(1) Open the following method:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender

(2) Pass the value in it

// In a storyboard-based application, you will often want to do a little preparation before navigation-(void) prepareForSegue :( UIStoryboardSegue *) segue sender :( id) sender {if ([segue. identifier ispolictostring: @ "aTob"]) {id theSegue = segue. destinationViewController; // assign A value to [theSegue setValue: @ "A page value" forKey: @ "your own KEY"] ;}}

-- The operations in BviewController are the same as those in the previous method.

 

Third: Use NSUserDefaults to pass the value;

 

-- In AViewController

Encoding of places where values need to be passed

// Store data NSUserDefaults * defaults = [NSUserDefaults standardUserDefaults]; [defaults setObject: @ "the value to be passed on the interface" forKey: @ "your KEY"]; // set synchronization [defaults synchronize];

 

-- "BViewController File

Encode and read the passed value in ViewDidLoad.

 

// Read the value NSUserDefaults * defaults = [NSUserDefaults standardUserDefaults] passed through interface A; self. userId = [defaults valueForKey: @ "your KEY"];

 

 

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.