iOS传值(block传值)

来源:互联网 发布:海盗湾中文域名 编辑:程序博客网 时间:2024/05/01 21:17

block传值相比于代理更简便。

A页面:

SecondViewController *second = [[SecondViewController alloc] initWithNibName:@"SecondViewController" bundle:nil];
    second.tempBlock = ^(NSString *str){
        self.myLabel.text = str;
    };
    [self presentViewController:second animated:YES completion:nil];


B页面.h


#import <UIKit/UIKit.h>

typedef void (^myBlock)(NSString *tempStr);

@interface SecondViewController : UIViewController

@property (nonatomic,strong) myBlock tempBlock;
@end


B页面.m

- (IBAction)popClick:(id)sender {
    if (self.tempBlock) {
        self.tempBlock(@"123");
    }
    [self dismissViewControllerAnimated:YES completion:nil];
}



0 0
原创粉丝点击