IOS uses block for counter-pass values

Source: Internet
Author: User

As with the notification is also two controllers, then the code creates the control directly on the code

#import "ViewController.h"

#import "TwoViewController.h"

@interface Viewcontroller ()

{

UIButton *_nextbtn;

UILabel *_showlabel;

}

@end

@implementation Viewcontroller

-(void) Viewdidload {

[Super Viewdidload];

[Self show];

}

-(void) show{

_NEXTBTN = [[UIButton alloc]initwithframe:cgrectmake (100, 100, 100, 100)];

[_nextbtn Settitlecolor:[uicolor Redcolor] forstate:uicontrolstatenormal];

[_nextbtn settitle:@ "Next" forstate:uicontrolstatenormal];

[_nextbtn addtarget:self Action: @selector (Nextbtnclick:) forcontrolevents:uicontroleventtouchupinside];

[Self.view ADDSUBVIEW:_NEXTBTN];

_showlabel = [[UILabel alloc]initwithframe:cgrectmake (100, 300, 100,50)];

[_showlabel Setbackgroundcolor:[uicolor Greencolor];

[_showlabel Settextcolor:[uicolor Redcolor];

[Self.view Addsubview:_showlabel];

}

-(void) Nextbtnclick: (UIButton *) nextbtnclick{

Twoviewcontroller * = [[Twoviewcontroller alloc]init];

Two.block = ^ (NSString * str) {

_showlabel.text = str;

};

[Self presentviewcontroller:two animated:yes completion:nil];

}

In the TwoViewController.h

#import <UIKit/UIKit.h>

typedef void (^myblock) (NSString *);

@interface Twoviewcontroller:uiviewcontroller

@property (nonatomic,copy) Myblock block;

@end

in the TWOVIEWCONTROLLER.M

#import "TwoViewController.h"

@interface Twoviewcontroller ()

{

UIButton *_backbtn;

Uitextfield *_textfield;

}

@end

@implementation Twoviewcontroller

-(void) Viewdidload {

[Super Viewdidload];

Self.view.backgroundColor = [Uicolor Bluecolor];

[Self Shoulabel];

}

-(void) shoulabel{

_BACKBTN = [[UIButton alloc]initwithframe:cgrectmake (100, 100, 100, 100)];

_backbtn.backgroundcolor = [Uicolor Redcolor];

[_backbtn settitle:@ "Shang" forstate:uicontrolstatenormal];

[_backbtn addtarget:self Action: @selector (Shangbtnclick:) forcontrolevents:uicontroleventtouchupinside];

[Self.view ADDSUBVIEW:_BACKBTN];

_textfield = [[Uitextfield alloc]initwithframe:cgrectmake (100, 400, 250, 35)];

_textfield.backgroundcolor = [Uicolor Yellowcolor];

[Self.view addsubview: _textfield];

}

-(void) Shangbtnclick: (UIButton *) shangbtn{

[Self Dismissviewcontrolleranimated:yes completion:^{

if (Self.block) {

Self.block (_textfield.text);

}

}];

}

This makes it simple to pass the value

IOS uses block for counter-pass values

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.