代码块传值

#import<UIKit/UIKit.h>

#import "SecondViewController.h"

//遵循协议

@interface ViewController : UIViewController

@property(strong,nonatomic) UITextField *textName;

@property(strong,nonatomic) UIButton *btn;

@end


#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {

[super viewDidLoad];

// Do any additional setup after loading the view, typically from a nib.

self.textName = [[UITextField alloc] initWithFrame:CGRectMake(100, 200, 100, 50)];

self.textName.borderStyle = 1;

[self.view addSubview:self.textName];

self.btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];

self.btn.frame = CGRectMake(200, 200, 50, 50);

[self.btn setTitle:@"下一页" forState:UIControlStateNormal];

[self.btn addTarget:self action:@selector(NextPage) forControlEvents:UIControlEventTouchUpInside];

[self.view addSubview:self.btn];

}

////实现协议方法

//-(void)postValue:(NSString *)info

//{

//    self.textName.text = info;

//}

-(BOOL)textFieldShouldReturn:(UITextField *)textField

{

if ([textField isFirstResponder]) {

[textField resignFirstResponder];

}

return YES;

}

-(void)NextPage

{

SecondViewController *second = [[SecondViewController alloc] init];

second.str = self.textName.text;

//实现代码块

second.myblock = ^(NSString *info){

self.textName.text = info;

};

[self presentViewController:second animated:YES completion:^{

NSLog(@"Next");

}];

}

- (void)didReceiveMemoryWarning {

[super didReceiveMemoryWarning];

// Dispose of any resources that can be recreated.

}

@end

/* 代码块传值 从后往前传

1.声明代码块 (secondXXX.h)

2.声明一个代码块类型的属性 (SecondXXX.h)

3.调用代码块 (secondXXX.m)

4.实现代码块 (firstXXX.m)

*/

#import<UIKit/UIKit.h>

typedef void(^postValueBlock)(NSString *info);

@interface SecondViewController : UIViewController

@property(strong,nonatomic) NSString *str;

@property(strong,nonatomic) UITextField *textInfo;

@property(strong,nonatomic) postValueBlock myblock;

@end

#import "SecondViewController.h"

@interface SecondViewController ()

@end

@implementation SecondViewController

- (void)viewDidLoad {

[super viewDidLoad];

// Do any additional setup after loading the view.

self.view.backgroundColor = [UIColor grayColor];

NSLog(@"%@",self.str);

self.textInfo = [[UITextField alloc] initWithFrame:CGRectMake(100, 150, 100, 50)];

self.textInfo.borderStyle = 1;

self.textInfo.delegate = self;

self.textInfo.text = self.str;

[self.view addSubview:self.textInfo];

}

//3.调用代理方法

-(BOOL)textFieldShouldReturn:(UITextField *)textField

{

//1.调用block

if (self.myblock) {

self.myblock(textField.text);

}

//2.键盘隐藏

if ([textField isFirstResponder]) {

[textField resignFirstResponder];

}

//3.隐藏页面

[self dismissViewControllerAnimated:YES completion:^{

NSLog(@"dismiss");

}];

return YES;

}

- (void)didReceiveMemoryWarning {

[super didReceiveMemoryWarning];

// Dispose of any resources that can be recreated.

}

/*

#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {

// Get the new view controller using [segue destinationViewController].

// Pass the selected object to the new view controller.

}

*/

@end

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容