block回调
-
情景:viewController2给viewController1传值,将viewController1的按钮名字改为“已点击”
-
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(@"已点击");
}
- viewController1.h代码
- (void)viewDidLoad {
[super viewDidLoad];
[self.btn setTitle:@"跳转" 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];
}
网友评论