ios block传值

ios里的block 可以理解为c++里的函数指针, 我觉得也有点儿像java里面的内部类之类的东西。

block可以代替delegate完成回调工作。

只需要把block当成地址传来传去就行了。

下面把某位仁兄的代码贴出来


用此方法传值可以替代委托了。具体例子
MainView.h
#import <UIKit/UIKit.h>

@interface MainView : UIViewController
{
    IBOutlet UIButton* btn;
    IBOutlet UILabel* labShow;
}
-(IBAction)push:(id)sender;
@end

MainView.m
 
#import "MainView.h"
#import "SecondView.h"

@implementation MainView

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
    }
    return self;
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
}
-(IBAction)push:(id)sender
{
    SecondView *s = [[SecondView alloc] initwithBlock:Block_copy(^(NSString *str){
        NSLog(@"%@",str);
        labShow.text = str;
    })];
    [self.navigationController pushViewController:s  animated:YES];
    [s release];
}
- (void)viewDidUnload
{
    [super viewDidUnload];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

@end

Second.h
 
#import <UIKit/UIKit.h>
typedef void (^MyBlock)(NSString *);

@interface SecondView : UIViewController
{
    IBOutlet UITextField* txtView;
    MyBlock my;
}
-(IBAction)back:(id)sender;
-(id)initwithBlock:(MyBlock)str;
@end

Second.m
 
#import "SecondView.h"

@implementation SecondView

-(id)initwithBlock:(MyBlock)str
{
    self = [super init];
    if(self)
    {   
        my = str;
    }
    return self;
}
-(IBAction)back:(id)sender
{
    NSString* s = txtView.text;
    if(my)
    {
        my(s);
    }
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}
-(void)dealloc{
    Block_release(my);
    [super dealloc];
    
}
#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
}

- (void)viewDidUnload
{
    [super viewDidUnload];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

@end







关键点:

1.执行点击事件会出发下面方法,切换到SecondView界面,并且将块元素(函数指针)传入SecondeView

-(IBAction)push:(id)sender
{
    SecondView *s = [[SecondView alloc] initwithBlock:Block_copy(^(NSString *str){
        NSLog(@"%@",str);
        labShow.text = str;
    })];
    [self.navigationController pushViewController:s  animated:YES];
    [s release];
}

2.在secondeView中:
typedef void (^MyBlock)(NSString *);
 MyBlock my;
-(id)initwithBlock:(MyBlock)str
{
    self = [super init];
    if(self)
    {   
        my = str;
    }
    return self;
}


initWithBlock方法被调用,并且将块元素保存到my(函数地址)

3.在SecondView中执行点击事件,会触发下面
-(IBAction)back:(id)sender
{
    NSString* s = txtView.text;
    if(my)
    {
        my(s);
    }
    [self.navigationController popViewControllerAnimated:YES];
}

这个方法拿着块元素(函数指针),并且将参数s传入块元素。



4.下面就是关键:
   my会根据块元素的地址,找到块元素的实现部分,也就是在MainView中最初传入的block_copy那里异步执行块实现部分。

这个过程,是不是代理干的事情,或者钩子,或者回调,我觉得这都是一回事。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值