Transfer value and proxy values for multiple views in iOS

Source: Internet
Author: User
Tags uikit

First create two classes, Firstviewcontroller and Secondviewcontroller, all inherited from Uiviewcontroller

1 #import "AppDelegate.h"2 #import "FirstViewController.h"3 4 @interfaceappdelegate ()5 6 @end7 8 @implementationappdelegate9 Ten  One-(BOOL) Application: (UIApplication *) application didfinishlaunchingwithoptions: (Nsdictionary *) launchoptions { A     //initializing the View controller -Firstviewcontroller *firstvc=[[Firstviewcontroller alloc]init]; -     //Setting the root view controller theSelf.window.rootviewcontroller=FIRSTVC; -     returnYES; -}
1 #import <UIKit/UIKit.h>2#import"SecondViewController.h"  3@interface firstviewcontroller:uiviewcontroller<uitextfielddelegate,posvluedelegate >4 @property (strong,nonatomic) Uitextfield *textname; 5 @end
1 #import "FirstViewController.h"2 3 @interfaceFirstviewcontroller ()4 5 @end6 7 @implementationFirstviewcontroller8 9- (void) Viewdidload {Ten [Super Viewdidload]; One     //set the color of the background view ASelf.view.backgroundcolor=[Uicolor Redcolor]; -     //Initialize -Self.textname=[[uitextfield Alloc] Initwithframe:cgrectmake ( -, Max, $, +)]; the     //set the color of a text box -Self.textname.backgroundcolor=[Uicolor Whitecolor]; -self.textname.borderstyle=1; - [Self.view addSubview:self.textName]; +      - } +-(void) Touchesbegan: (Nsset<uitouch *> *) touches withevent: (Uievent *)Event A { atSecondviewcontroller *secondvc=[[Secondviewcontroller alloc] init]; -     //Set property value to pass value to Secondviewcontroller -Secondvc.str=Self.textName.text; -     //go to page two -[Self PRESENTVIEWCONTROLLER:SECONDVC animated:yes completion:^{ -NSLog (@"Firstviewcontroller Switch to Secondviewcontroller"); in     }]; -     //Specify proxy toSECONDVC.Delegate=Self ; + } - //Implementing protocol Methods the-(void) Posvlue: (NSString *) Str * { $self.textname.text=str;Panax Notoginseng } -  the  +  A  the  +  -  $- (void) didreceivememorywarning { $ [Super didreceivememorywarning]; -     //Dispose of any resources the can be recreated. -}
1 #import<UIKit/UIKit.h>2 //Create an agreement3 @protocolPosvluedelegate<nsobject>4-(void) Posvlue: (NSString *) str;//protocol Method5 6 @end7 8 @interfaceSecondviewcontroller:uiviewcontroller9@property (strong,nonatomic) Uitextfield *Textname;Ten@property (strong,nonatomic) NSString *str; One //Create a property of a protocol type A@property (assign,nonatomic)ID<posVlueDelegate>Delegate; - @end
1 #import "SecondViewController.h"2 3 @interfaceSecondviewcontroller ()4 5 @end6 7 @implementationSecondviewcontroller8 9- (void) Viewdidload {Ten [Super Viewdidload]; OneSelf.view.backgroundcolor=[Uicolor Graycolor]; ASelf.textname=[[uitextfield Alloc] Initwithframe:cgrectmake ( -, Max, $, -)]; -Self.textname.backgroundcolor=[Uicolor Whitecolor]; -self.textname.borderstyle=1; the [Self.view addSubview:self.textName]; -     //accept the value Firstviewcontroller passed over -self.textname.text=Self.str; - } +-(void) Touchesbegan: (Nsset<uitouch *> *) touches withevent: (Uievent *)Event - { +[Self Dismissviewcontrolleranimated:yes completion:^{ ANSLog (@"Secondviewcontroller Switch to Firstviewcontroller"); at     }]; -      -     if(self.)Delegate) { -[Self.DelegatePosVlue:self.textName.text]; -     } - } in  -  to- (void) didreceivememorywarning { + [Super didreceivememorywarning]; -     //Dispose of any resources the can be recreated. the}

Summarize:

1. How to transfer values between pages

Property pass-through value

Suitable for forward-passing values

1.1 Create a property on the page where you want to display the information

1.2 Setting the property value on the page to which the value is to be passed

1.3 In the Viewdidload method that displays the page, accept the property value

Proxy pass-through value

Suitable for reverse pass-through values

1. Create the Protocol and protocol method, in the reverse value of the page Secondviewcontroller

2. Create a property of the protocol type, create a property in Secondviewcontroller

@property (assign,nonatomic) id<posvluedelegate> delegate;

3. Call property is delegate

Secondviewcontroller in the method of passing the value of an object in a page

if (self.delegate) {

[Self.delegate PosVlue:self.textName.text];

}

4. On the first page, the page that changes the rich information is displayed.

To follow the Protocol implementation protocol method to specify a proxy object (that is, to assign a value to the agent in the method of the page pass parameter-(void) Posvlue: (NSString *) str

{

SELF.TEXTFIELD.TEXT=STR;

})

Transfer value and proxy values for multiple views in iOS

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.