UIButton block equivalent to addTarget:action:forControlEvents: method?

后端 未结 9 1217
旧巷少年郎
旧巷少年郎 2020-11-29 20:58

I looked around, but couldn\'t find this on the internet, nor anywhere in the Apple docs, so I\'m guessing it doesn\'t exist.

But is there a iOS4 blocks equivalent A

9条回答
  •  孤城傲影
    2020-11-29 21:38

    I just implemented this. It work's like a charm!

    And it wasn't even hard.

    typedef void (^ActionBlock)();
    
    @interface UIBlockButton : UIButton {
        ActionBlock _actionBlock;
    }
    
    -(void) handleControlEvent:(UIControlEvents)event
                     withBlock:(ActionBlock) action;
    @end
    
    @implementation UIBlockButton
    
    -(void) handleControlEvent:(UIControlEvents)event
                     withBlock:(ActionBlock) action
    {
        _actionBlock = action;
        [self addTarget:self action:@selector(callActionBlock:) forControlEvents:event];
    }
    
    -(void) callActionBlock:(id)sender{
        _actionBlock();
    }
    @end
    

提交回复
热议问题