block回調(diào)
情景:viewController2給viewController1傳值沧竟,將viewController1的按鈕名字改為“已點(diǎn)擊”
-
viewController2.h代碼
import <UIKit/UIKit.h>
typedef void(^Myblock)(NSString *);
@interface ViewController2 : UIViewController
@property(nonatomic,copy) Myblock firstBlock;
@end
- viewController2.m代碼
- (void)viewDidLoad {
[super viewDidLoad];
self.firstBlock(@"已點(diǎn)擊");
}
- viewController1.h代碼
- (void)viewDidLoad {
[super viewDidLoad];
[self.btn setTitle:@"跳轉(zhuǎn)" forState:UIControlStateNormal];
- viewController1.m代碼
- (IBAction)click:(UIButton )sender {
ViewController2two = [[ViewController2 alloc]init];
two.frstBlock = ^(NSString * name) {
[_btn setTitle:name forState:UIControlStateNormal];
NSLog(@"peibobo---%@",name);
};
[self presentViewController:two animated:YES completion:nil];
}