#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.調用代碼塊 (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.調用代理方法
-(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