美文网首页
简单block回调

简单block回调

作者: 邹邹_ZZ | 来源:发表于2016-11-01 15:03 被阅读32次

    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 {
      ViewController2
      two = [[ViewController2 alloc]init];
      two.frstBlock = ^(NSString * name) {
      [_btn setTitle:name forState:UIControlStateNormal];
      NSLog(@"peibobo---%@",name);
      };
      [self presentViewController:two animated:YES completion:nil];
      }

    相关文章

      网友评论

          本文标题:简单block回调

          本文链接:https://www.haomeiwen.com/subject/wgymuttx.html