iOS development--Proxy and block pass value

Source: Internet
Author: User
Tags uikit

One, the method of the proxy transfer value

In 1.hehe1viewcontroller.h

#import <UIKit/UIKit.h>

@protocol hehe1viewcontrollerdelegate <NSObject>

-(void) Backvaluewith: (nsstring*) str;

@end

@interface Hehe1viewcontroller:uiviewcontroller

@property (nonatomic,weak) ID delegate;

@end

In 2.HEHE1VIEWCONTROLLER.M

#import "Hehe1ViewController.h"

@interface Hehe1viewcontroller ()

@property (Nonatomic,strong) Uitextfield *hehetextfield;

@end

@implementation Hehe1viewcontroller

-(void) Viewdidload {

[Super Viewdidload];

Uitextfield *hehetextfield = [[Uitextfield alloc] Initwithframe:cgrectmake (20, 200, 300, 40)];

Self.hehetextfield = Hehetextfield;

Hehetextfield.backgroundcolor = [Uicolor Lightgraycolor];

[Self.view Addsubview:hehetextfield];

UIButton *button1 = [UIButton Buttonwithtype:uibuttontypesystem];

Button1.frame = CGRectMake (20, 250, 300, 50);

Button1.backgroundcolor = [Uicolor Orangecolor];

[Button1 settitle:@ "Back to VC" forstate:uicontrolstatenormal];

[button1 Settintcolor:[uicolor Whitecolor];

[Button1 addtarget:self Action: @selector (GOTOHEHE1) forcontrolevents:uicontroleventtouchupinside];

[Self.view Addsubview:button1];

}

-(void) GoToHehe1 {

NSLog (@ "hehe1 ...");

[_delegate BackValueWith:self.heheTextField.text];

[Self.navigationcontroller Popviewcontrolleranimated:yes];

}

@end

Second, block transmission value

In 1.hehe2viewcontroller.h

#import <UIKit/UIKit.h>

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

@interface Hehe2viewcontroller:uiviewcontroller

@property (nonatomic,copy) Backvalue backvalue;

-(void) Returnstr: (backvalue) block;

@end

In 2.HEHE2VIEWCONTROLLER.M

#import "Hehe2ViewController.h"

@interface Hehe2viewcontroller ()

@property (Nonatomic,strong) Uitextfield *hehetextfield;

@end

@implementation Hehe2viewcontroller

-(void) Viewdidload {

[Super Viewdidload];

Uitextfield *hehetextfield = [[Uitextfield alloc] Initwithframe:cgrectmake (20, 200, 300, 40)];

Self.hehetextfield = Hehetextfield;

Hehetextfield.backgroundcolor = [Uicolor Lightgraycolor];

[Self.view Addsubview:hehetextfield];

UIButton *button2 = [UIButton Buttonwithtype:uibuttontypesystem];

Button2.frame = CGRectMake (20, 250, 300, 50);

Button2.backgroundcolor = [Uicolor Orangecolor];

[Button2 settitle:@ "Back to VC" forstate:uicontrolstatenormal];

[Button2 Settintcolor:[uicolor Whitecolor];

[Button2 addtarget:self Action: @selector (GOTOHEHE2) forcontrolevents:uicontroleventtouchupinside];

[Self.view Addsubview:button2];

}

-(void) Returnstr: (backvalue) Block {

Self.backvalue = Block;

}

-(void) GoToHehe2 {

NSLog (@ "Hehe2 ...");

Self.backvalue (Self.heheTextField.text);

[Self.navigationcontroller Popviewcontrolleranimated:yes];

}

@end

Third, receive the value returned in the VIEWCONTROLLER.M

#import "ViewController.h"

#import "Hehe1ViewController.h"//delegate

#import "Hehe2ViewController.h"//block

#define Klsceenwidth Self.view.bounds.size.width

@interface Viewcontroller ()

@property (Nonatomic,strong) UILabel *label1;

@property (Nonatomic,strong) UILabel *label2;

@end

@implementation Viewcontroller

-(void) Viewdidload {

[Super Viewdidload];

UILabel *label1 = [[UILabel alloc] Initwithframe:cgrectmake (klSceenWidth-40, 30)];

Self.label1 = Label1;

[Self.view Addsubview:label1];

Delegate

UIButton *button1 = [UIButton Buttonwithtype:uibuttontypesystem];

Button1.frame = CGRectMake (klSceenWidth-40, 30);

Button1.backgroundcolor = [Uicolor Orangecolor];

[Button1 settitle:@ "Go to Hehe1" forstate:uicontrolstatenormal];

[button1 Settintcolor:[uicolor Whitecolor];

[Button1 addtarget:self Action: @selector (GOTOHEHE1) forcontrolevents:uicontroleventtouchupinside];

[Self.view Addsubview:button1];

Block

UILabel *label2 = [[UILabel alloc] Initwithframe:cgrectmake (klSceenWidth-40, 30)];

Self.label2 = Label2;

[Self.view Addsubview:label2];

UIButton *button2 = [UIButton Buttonwithtype:uibuttontypesystem];

Button2.frame = CGRectMake (klSceenWidth-40, 30);

Button2.backgroundcolor = [Uicolor Orangecolor];

[Button2 settitle:@ "Go to Hehe1" forstate:uicontrolstatenormal];

[Button2 Settintcolor:[uicolor Whitecolor];

[Button2 addtarget:self Action: @selector (GOTOHEHE2) forcontrolevents:uicontroleventtouchupinside];

[Self.view Addsubview:button2];

}

-(void) Backvaluewith: (nsstring*) str {

Self.label1.text = str;

}

-(void) GoToHehe1 {

Hehe1viewcontroller *hehevc1= [[Hehe1viewcontroller alloc] init];

Hehevc1.delegate = self;

[Self.navigationcontroller pushviewcontroller:hehevc1 Animated:yes];

}

-(void) GoToHehe2 {

Hehe2viewcontroller *hehevc2= [[Hehe2viewcontroller alloc] init];

[HeheVC2 returnstr:^ (NSString *str) {

Self.label2.text = str;

}];

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

}

@end

iOS development--Proxy and block pass value

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.