iOS Page Pass value

Source: Internet
Author: User

How to transfer values between pageswith Firstview and secondview two viewsProperty pass-through values (for forward values between pages) 1, create a property 2 on the page where you want to display the information, set the property value 3 on the page where you want to send the value, and in the Viewdidload method of the page that displays the information, receive the property value proxy value (for the reverse value between pages) 1, create the Protocol, In the reverse pass-through page (SECONDVC) 2, create a property of the protocol type, create an attribute 3 in SECONDVC, invoke the property as delegate, and Invoke [Self.delegate Postvalue] in the method of passing the value of the object in the SECONDVC page:   Self.textname.text];4, on the first page, the page that displays the modified information follows the protocol, implements the method of the Protocol, and specifies the proxy (that is, assigning a value to the agent in the method of the page pass parameter) second.delegate=self; code block passing value (for reverse value) 1, declaring a code block (second###) 2, declaring a code block Type property (second###) 3, calling code block (second###) 3, Implementing code block (first###)ViewController.h

Declare a text box and a button

@property (strong,nonatomic) Uitextfield *textname;

@property (strong,nonatomic) UIButton *button;

Viewcontroller.m

-(void) Viewdidload {

[Super Viewdidload];

Additional setup after loading the view, typically from a nib.

Self.textname=[[uitextfield Alloc] Initwithframe:cgrectmake (100, 100, 150, 40)];

self.textname.borderstyle=1;

Self.button=[[uibutton Alloc] Initwithframe:cgrectmake (100, 200, 100, 40)];

Self.button.backgroundcolor=[uicolor Redcolor];

[Self.button settitle:@ "button" forstate:uicontrolstatenormal];

[Self.button addtarget:self Action: @selector (NextPage) forcontrolevents:uicontroleventtouchupinside];

[Self.view AddSubview:self.textName];

[Self.view AddSubview:self.button];

Specify Proxy

self.textname.delegate=self;

}

-(void) nextPage

{

Secondviewcontroller *second=[[secondviewcontroller alloc] init];

Second.str=self.textname.text;

Agent

second.delegate=self;

second.myblock=^ (NSString *str)

{

SELF.TEXTNAME.TEXT=STR;

};

[Self Presentviewcontroller:second animated:yes completion:^{

NSLog (@ "Go to next page");

}];

}

Implementation of the Protocol method

-(void) Postvalue: (NSString *) value

{

Self.textname.text=value;

}

SecondViewController.h

typedef void (^valueblock) (NSString *str);

Protocol in proxy pass-through value

@protocol postvaluedelegate <NSObject>

-(void) Postvalue: (NSString *) value;

@end

Follow the agreement

@interface secondviewcontroller:uiviewcontroller<uitextfielddelegate>

@property (strong,nonatomic) NSString *str;

@property (strong,nonatomic) Uitextfield *txt2;

@property (strong,nonatomic) UIButton *button2;

An attribute defined with a protocol

@property (strong,nonatomic) id<postvaluedelegate> delegate;

A property of a code block definition

@property (strong,nonatomic) Valueblock Myblock;

Secondviewcontroller.m

-(void) Viewdidload {

[Super Viewdidload];

Do any additional setup after loading the view.

Self.view.backgroundcolor=[uicolor Graycolor];

Self.txt2=[[uitextfield Alloc] Initwithframe:cgrectmake (100, 100, 150, 40)];

self.txt2.borderstyle=1;

[Self.view ADDSUBVIEW:SELF.TXT2];

Self.button2=[[uibutton Alloc] Initwithframe:cgrectmake (100, 200, 100, 40)];

[Self.button2 settitle:@ "return" forstate:uicontrolstatenormal];

[Self.button2 addtarget:self Action: @selector (BackPage) forcontrolevents:uicontroleventtouchupinside];

[Self.view AddSubview:self.button2];

Property pass-through value

SELF.TXT2.TEXT=SELF.STR;

NSLog (@ "str=%@", self.str);

self.txt2.delegate=self;

}

-(void) backPage

//{

Proxy pass-through value

if (self.delegate)

//    {

[Self.delegate PostValue:self.txt2.text];

//    }

//

//

[Self Dismissviewcontrolleranimated:yes completion:^{

NSLog (@ "return");

//    }];

//}

-(BOOL) Textfieldshouldreturn: (Uitextfield *) TextField

{

Code block pass-through value

if (Self.myblock)

{

Self.myblock (Textfield.text);

}

if ([TextField Isfirstresponder])

{

[TextField Resignfirstresponder];

}

[Self Dismissviewcontrolleranimated:yes completion:^{

NSLog (@ "return");

}];

return YES;

}

In addition to the above three methods of transmission, there are single-pass values, notification value, update ...

iOS Page Pass value

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.