Transfer value of iOS agent

Source: Internet
Author: User

There are several ways of transmitting value in iOS, namely: Proxy value, block pass value, attribute pass value, notification pass value, single case pass value, use Userdefault or file way to transmit value, usually proxy value and block pass value use most commonly, this article introduces the way of proxy value, Subsequent blogs will write other values at once.

What is a principal agent?

1, the Agreement (Protocol), is the use of this agreement, must be in accordance with the provisions of the Agreement to deal with the matter, the method required by the agreement must be implemented (except @optional method).

Protocol is a syntax that provides a handy opportunity to implement delegate patterns.

The definition protocol is as follows:

    1. @protocol classbdelegate<nsobject>
    2. -(void) MethodOne;
    3. @optional
    4. -(void) Methodtwo: (NSString *) value;
    5. @end

Defines a CLASSB protocol that contains two methods, where methodtwo is optional.

Implement this protocol in ClassA's header file (ClassA.h), as in the following code:

    1. @interface classa<classbdelegate>
    2. @end

In the ClassA implementation file (CLASSA.M) Implementation of the Classbdelegate two methods, where methodtwo can not be implemented, as follows:

    1. -(void) methodone{
    2. //Concrete implementation content
    3. }
    4. -(void) Methodtwo: (NSString *) value{
    5. //Concrete implementation content
    6. }

2, the agent (delegate), as the name implies is entrusted to others to do things, when a thing happened, they do not handle, let others to deal with.

Delegate and protocol are not related. Delegate itself is a design pattern. is to take one part of a class that you need to do, so that another class (or itself) can do it.

Define a proxy in the ClassB header file (ClassB.h) as follows:

    1. @interface ClassB
    2. @property (nonatomic, unsafe_unretained) id<classbdelegate> delegate;
    3. @end

That way, when we encounter problems in the ClassB implementation file (CLASSB.M) that you want other classes (such as ClassA) to handle, you can

    1. [Self.delegate MethodOne];
    2. [Self.delegate methodtwo:@"value to be passed"];

Two concrete examples

The function implemented is to create a UIButton button in Viewcontroller for push to the next page, and a uilable to display the text returned from the second page, in second Create a Uitextfield in Viewcontroller to enter text. After the input finishes pressing back to return to the first page, the input text is displayed on the lable.

1 Engineering

2 ViewController.h File

#import <UIKit/UIKit.h>

@interface Viewcontroller:uiviewcontroller

@end

3 viewcontroller.m File

#import "ViewController.h"

#import "SecondViewController.h"

@interface Viewcontroller () <getTextFromDelegate>

{

UIButton *_button;

UILabel *_lable;

}

@end

@implementation Viewcontroller

-(void) Viewdidload {

[Super Viewdidload];

[Self initlayout];

}

-(void) initlayout{

_button = [UIButton Buttonwithtype:uibuttontypesystem];

_button.frame = CGRectMake (0, 0, 100, 50);

_button.center = Self.view.center;

_button.backgroundcolor = [Uicolor Redcolor];

[_button addtarget:self Action: @selector (Pushtonextviewcontroller:) forcontrolevents:uicontroleventtouchupinside] ;

[_button settitle:@ "Next page" Forstate:uicontrolstatenormal];

[Self.view Addsubview:_button];

_lable = [[UILabel alloc]initwithframe:cgrectmake (10, 64, 355, 200)];

_lable.backgroundcolor = [Uicolor Orangecolor];

[Self.view addsubview:_lable];

}

-(void) Pushtonextviewcontroller: (UIButton *) sender{

Secondviewcontroller *SECONDVC = [Secondviewcontroller new];

The agent is itself.

Secondvc.delegate = self;

[Self.navigationcontroller PUSHVIEWCONTROLLER:SECONDVC Animated:yes];

}

#pragma mark Implementation Proxy method

-(void) GetText: (NSString *) text{

_lable.text = text;

}

-(void) didreceivememorywarning {

[Super didreceivememorywarning];

Dispose of any resources the can be recreated.

}

@end

4 SecondViewController.h File

#import <UIKit/UIKit.h>

@protocol gettextfromdelegate <NSObject>

Declaring protocol methods

-(void) GetText: (NSString *) text;

@end

@interface Secondviewcontroller:uiviewcontroller

@property (Weak, nonatomic) id<gettextfromdelegate>delegate;

@end

4 secondviewcontroller.m File

#import "SecondViewController.h"

@interface Secondviewcontroller () <getTextFromDelegate>

{

Uitextfield *_textfield;

}

@end

@implementation Secondviewcontroller

-(void) Viewdidload {

[Super Viewdidload];

Self.view.backgroundColor = [Uicolor Whitecolor];

_textfield = [[Uitextfield alloc]initwithframe:cgrectmake (0, 0, 200, 50)];

_textfield.backgroundcolor = [Uicolor Redcolor];

_textfield.center = Self.view.center;

[Self.view Addsubview:_textfield];

}

#pragma mark when the page is going to disappear

-(void) Viewwilldisappear: (BOOL) animated{

Pass the values obtained from this page to the previous page to implement

[Self.delegate Gettext:_textfield.text];

}

-(void) didreceivememorywarning {

[Super didreceivememorywarning];

Dispose of any resources the can be recreated.

}

@end

Three simulator Run results

Transfer value of iOS agent

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.