代碼塊傳值

#import<UIKit/UIKit.h>

#import "SecondViewController.h"

//遵循協(xié)議

@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];

}

////實現(xiàn)協(xié)議方法

//-(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;

//實現(xiàn)代碼塊

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.調(diào)用代碼塊 (secondXXX.m)

4.實現(xiàn)代碼塊 (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.調(diào)用代理方法

-(BOOL)textFieldShouldReturn:(UITextField *)textField

{

//1.調(diào)用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

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容