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"];