Attribute transfer value, protocol pass value, block pass value, single-pass value of four kinds of interface transfer value mode

Source: Internet
Author: User

First, the value of the property passed

For the value of the property, compared to the other three methods, is the most basic, the simplest method, but the value of the property is very limited, because it is applicable to the first interface to the second boundary value, the second to the third interface to pass value and so on. n bounds for n + 1 interface values. On this basis, we must know the explicit position of the jump interface and the specific type of the value to be transmitted. In the second interface, declare the property of the type you want to pass the value to.

@interface Secondviewcontroller:uiviewcontroller // declares a string property to hold the string content passed over the first interface @property (nonatomic, copy) NSString *string**label;

Of course, using the declared property in the second interface

-(void) viewdidload {    [super Viewdidload];         = _color;     *button = [UIButton buttonwithtype:uibuttontypesystem];     = CGRectMake (+);    [Button settitle:self. string forstate:uicontrolstatenormal];

When a property is passed a value, you need to know the location of the explicit jump and the type of value to pass.

@implementation Firstviewcontroller-(void) right{    *SECONDVC = [[Secondviewcontroller alloc] Init ];    SECONDVC. string = self.textField.text;     = Self.view.backgroundColor;    [Self.navigationcontroller PUSHVIEWCONTROLLER:SECONDVC animated:yes];}

Second, the protocol transmission value

In general, protocol values can be used in conjunction with attribute passing values, which are passed from one value to the next, and the protocol is passed from the back to the value.

Protocol value can be easily divided into six steps to achieve.

1. Declaration of Agreement 2. Proxy 3. Execute protocol Method 4. Receive Protocol 5. Set protocol proxy object 6. Implementing Protocol Methods

 // 1. Declaration Protocol   @protocol  Fourviewcontrollerdelegate <nsobject>-(void ) Change: (NSString *) string  ;  -(void ) ChangeColor: (Uicolor *) color;   @end   @interface   Fourviewcontroller:uiviewcontroller@property (nonatomic, retain) Uitextfield  *textfield;  // 2. Claims proxy  @property (nonatomic, assign) id  <fourviewcontrollerdelegate>foudelegate;   @end  
@implementation Fourviewcontroller-(void) back{    //3. Execute protocol method    if ( Self.foudelegate! = Nil && [self.foudelegate respondstoselector: @selector (change:)]) {        [ Self.foudelegate Change:self.textField.text];        [Self.foudelegate ChangeColor:self.view.backgroundColor];    }        [Self.navigationcontroller Popviewcontrolleranimated:yes];}

And if you want to use the protocol, you have to receive the protocol

// 5. Receiving Agreement @interface Threeviewcontroller:uiviewcontroller<fourviewcontrollerdelegate>*label; @end
1 @implementationThreeviewcontroller2 3 //6. Implementing the Protocol approach4- (void) Change: (NSString *)string{5Self.label.text =string;6 }7- (void) ChangeColor: (Uicolor *) color{8Self.view.backgroundColor =color;9 }Ten-(void) button{ OneFourviewcontroller *FOURVC =[[Fourviewcontroller alloc] init]; A     //4. Specify a proxy object for the second interface as the first view controller -Fourvc.foudelegate =Self ; - [Self.navigationcontroller pushviewcontroller:fourvc animated:yes]; the}

Third, block transmission value

As compared to the first two, it should be known that block is an anonymous function. Since it is a function, there will be four forms of expression, no parameter, no return value, there is no return value of the parameter, there is no return value of the parameter, there are four forms of the return value. As a result, blocks behave in a variety of ways relative to the first two.

1__blockintA =0;2     void(^BLOCK1) (void) = ^(void){3A + +;4NSLog (@"%d", a);5     };6 Block1 ();7     8     void(^BLOCK2) (intAge, NSString *string) = ^(intAge, NSString *string){9NSLog (@"Age was%d, text is%@", Age,string);Ten     }; OneBlock2 ( -,@"xiaoming");

ANSString * (^BLOCK3) (void) = ^(void){ - return @"with the Wu return value"; - }; theNSLog (@"Block3 is%@", Block3 ()); -NSString * (^BLOCK4) (nsstring *text) = ^ (NSString *string){ - return[stringStringbyappendingstring:@"has a return value"]; - }; +NSLog (@"Block4 is%@", Block4 (@"have a parameter return value"));

Block and protocol pass the function similar, but different from the protocol value of cumbersome, block has very good operability.

The second interface declares the block

#import <UIKit/UIKit.h>void (^badablock) (NSString *void (^ Colorblock) (Uicolor *); @interface  *TextField; @property (nonatomic, copy) Badablock Bada; @property (nonatomic, copy) Colorblock Cblock; @end

Block is declared as a property, be sure to use copy
Because the contents of block storage are in the stack area, copying one copy to the heap area with copy is not possible because the retain can only add 1 to the reference count in the heap, but not to add 1 to the reference count of the stack area.

Executes the declared Blcok method in the second interface

@implementation Secondviewcontroller-(void) back{    // execute block        if ( Self.bada! = nil) {        Self.bada (self.textField.text)    ;        } if (Self.cblock! = nil) {        self.cblock (self.view.backgroundColor);    }    [Self.navigationcontroller Popviewcontrolleranimated:yes];}

Block -specific release method block_release(_bada);

Block_release(_cblock);

Use block in the first interface

@implementation Firstviewcontroller-(void) button{    *SECONDVC = [[Secondviewcontroller alloc] INIT];     = ^ (NSString *str) {        = str;    };     = ^ (Uicolor *color) {        = color;    };    [Self.navigationcontroller PUSHVIEWCONTROLLER:SECONDVC Animated:yes];    [SECONDVC release];}

Iv. single-Case transmission value

Singleton, simply speaking, is a single instance object, no matter how many times it is created, it is unique and has only one.

The use of a single case is not limited, but need to know the location of the explicit jump, and the type of the required value.

In the case of a singleton, there are definitions within the system, such as UIScreen, Uidevice, which are defined within the system, and are typically user-defined single cases

#import <Foundation/Foundation.h>@interface*string; // The singleton method is a class method, and the return value type is Instancetype // a singleton class that is defined by itself, the method name usually starts with share +(instancetype) shareinstance; @end

Implementing the declared Singleton method

#import " Handler.h " Static Handler *handler = nil; @implementation Handler+(instancetype) shareinstance{    // lock to ensure that the creation process can only allow one thread object access at the same time     @synchronized (self) {    if (nil = = handler)        {= [[Handler Alloc] INIT];       }    }     return handler;} @end

When implementing the declaration of a singleton method, be sure to ensure that the singleton created is unique.

Create a singleton where the jump is implemented, assigning the value to the properties of the Singleton class declaration.

-(void) getaction{    *TWOVC = [[Twoviewcontroller alloc] init];         // Create a single case    Handler *handler = [Handler shareinstance];    Handler. string = _textfield.text;        [Self.navigationcontroller PUSHVIEWCONTROLLER:TWOVC animated:yes];}

Using a single value passed

@implementation Threeviewcontroller-(void) viewdidload {    [super Viewdidload];               *handler = [Handler shareinstance];     *label = [[UILabel alloc] Initwithframe:cgrectmake (+)];    = handler. string;

Attention:

Because the singleton can be used globally, it is very convenient and unique. So why not use it a lot? Of course not a lot of use, because it is a global can be used, equivalent to static, once created, will not be reclaimed by the system, so this area of memory will persist until the program exits, if a large number of single cases, will cause a lot of memory space wasted. Can be used, but cannot be used in large quantities.

Attribute transfer value, protocol pass value, block pass value, single-pass value of four kinds of interface transfer value mode

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.