UIButton块相当于addTarget:action:forControlEvents:method?

时间:2010-10-11 16:16:31

标签: iphone uibutton objective-c-blocks

我环顾四周,但在互联网上找不到这个,也没有在Apple文档的任何地方找到,所以我猜它不存在。

但是有一个iOS4阻止等效的API:

[button addTarget:self action:@selector(tappy:) forControlEvents:UIControlEventTouchUpInside];

我想这可以使用类别来实现,但由于极端懒惰,我宁愿不自己写这个:)

这样的事情会很棒:

[button handleControlEvent:UIControlEventTouchUpInside withBlock:^ { NSLog(@"I was tapped!"); }];

9 个答案:

答案 0 :(得分:53)

我刚刚实现了这个。它的工作就像一个魅力!

它甚至都不难。

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

答案 1 :(得分:23)

常见的Foundation / UI类增加了一个块库:BlocksKit。这是documentation

它不是UIButton的子类,但添加了UIControl category

[button addEventHandler:^(id sender) {
    //do something
} forControlEvents:UIControlEventTouchUpInside];

还有集合(地图,过滤器等),视图相关内容等的块/功能添加。

注意:它与Swift不兼容。

答案 2 :(得分:22)

这是一个工作类别实现。在它的当前形式中,这应该只在DEBUG中使用。当用户交互和时间很重要时,我将此类与功能(包含在下面)一起用于测试各种代码。同样,这仅用于开发/调试目的,不应考虑用于生产,因此#ifdef DEBUG;)

#ifdef DEBUG

#import <objc/runtime.h>

static char UIButtonBlockKey;

@interface UIButton (UIBlockButton)

- (void)handleControlEvent:(UIControlEvents)event withBlock:(ActionBlock)block;
- (void)callActionBlock:(id)sender;

@end


@implementation UIButton (UIBlockButton)

- (void)handleControlEvent:(UIControlEvents)event withBlock:(ActionBlock)block {
    objc_setAssociatedObject(self, &UIButtonBlockKey, block, OBJC_ASSOCIATION_COPY_NONATOMIC);
    [self addTarget:self action:@selector(callActionBlock:) forControlEvents:event];
}


- (void)callActionBlock:(id)sender {
    ActionBlock block = (ActionBlock)objc_getAssociatedObject(self, &UIButtonBlockKey);
    if (block) {
        block();
    }
}

@end


void DSAddGlobalButton(NSString *title, ActionBlock block) {
    UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [button setTitle:title forState:UIControlStateNormal];
    [button handleControlEvent:UIControlEventTouchUpInside withBlock:block];
    [button sizeToFit];
    [button setFrame:(CGRect){{100.0f, 100.0f}, [button frame].size}];

    UIView *firstView = [[[[UIApplication sharedApplication] keyWindow] subviews] objectAtIndex:0];
    [firstView addSubview:button];
}


#endif

答案 3 :(得分:7)

我创建了一个库来做这件事!

它支持UIControlUIButton),UIBarButtonItemUIGestureRecognizer。使用CocoaPods也支持它。

https://github.com/lavoy/ALActionBlocks

// Assuming you have a UIButton named 'button'
[button handleControlEvents:UIControlEventTouchUpInside withBlock:^(id weakControl) {
    NSLog(@"button pressed");
}];

安装

pod 'ALActionBlocks'

答案 4 :(得分:7)

快捷键4

这是快速解决方案

class ClosureSleeve {
let closure: () -> ()

init(attachTo: AnyObject, closure: @escaping () -> ()) {
    self.closure = closure
    objc_setAssociatedObject(attachTo, "[\(arc4random())]", self, .OBJC_ASSOCIATION_RETAIN)
}

@objc func invoke() {
    closure()
  }
}

extension UIControl {
func addAction(for controlEvents: UIControlEvents = .primaryActionTriggered, action: @escaping () -> ()) {
    let sleeve = ClosureSleeve(attachTo: self, closure: action)
    addTarget(sleeve, action: #selector(ClosureSleeve.invoke), for: controlEvents)
 }
}

示例用法:

button.addAction {
print("button pressed")
}

答案 5 :(得分:5)

我彻底解决了这个问题,而不是解决这个问题的方法!对UIButton进行子类化会创建一个不值得的雷区。使用Shayne Sweeney的分类(我刚刚通过一系列调整更新了他的答案,以使他的示例生产准备就绪......希望他们能够快速获得批准)。

----- ORIG POST -----

如果您只分配UIControlEventTouchUpInside,那么Martin发布的代码应该有效......但是有一些问题:

  • 如果您多次调用handleControlEvent,您将使用已发布的代码泄漏块。
  • 如果您指定了多种类型的事件,则会触发所有事件的最后一个块

在我的代码中,我依赖于块被视为object-c对象,它只适用于iOS4 +(而不是3.2)。当我想为按钮状态(即动画)做一些特殊的事情时,它对我很有用。您只需使用clickedButton块来处理正常点击。

#import <UIKit/UIKit.h>

@interface ButtWithBlockActions : UIButton {
  void (^downBlock_)(void);
  void (^upBlock_)(void);
  void (^clickedBlock_)(void);
}

@property(nonatomic,retain) void (^downBlock)(void);
@property(nonatomic,retain) void (^upBlock)(void);
@property(nonatomic,retain) void (^clickedBlock)(void);

@end



#import "ButtWithBlockActions.h"

@implementation ButtWithBlockActions

- (void)dealloc {
  [downBlock_ release];
  [upBlock_ release];
  [clickedBlock_ release];
  [super dealloc];
}


- (void (^)(void))downBlock { return downBlock_; }
- (void) fireDownBlock { downBlock_(); }
- (void) setDownBlock:(void (^)(void))block {
  if(downBlock_) {
    [self removeTarget:self action:@selector(fireDownBlock) forControlEvents:UIControlEventTouchDown];
    [self removeTarget:self action:@selector(fireDownBlock) forControlEvents:UIControlEventTouchDragEnter];
    [downBlock_ release];
  }
  downBlock_ = [block copy];
  if(downBlock_) {
    [self addTarget:self action:@selector(fireDownBlock) forControlEvents:UIControlEventTouchDown];
    [self addTarget:self action:@selector(fireDownBlock) forControlEvents:UIControlEventTouchDragEnter];
  }
}


- (void (^)(void))upBlock { return upBlock_; }
- (void) fireUpBlock { upBlock_(); }
- (void) setUpBlock:(void (^)(void))block {
  if(upBlock_) {
    [self removeTarget:self action:@selector(fireUpBlock) forControlEvents:UIControlEventTouchUpInside];
    [self removeTarget:self action:@selector(fireUpBlock) forControlEvents:UIControlEventTouchUpOutside];
    [self removeTarget:self action:@selector(fireUpBlock) forControlEvents:UIControlEventTouchDragOutside];
    [self removeTarget:self action:@selector(fireUpBlock) forControlEvents:UIControlEventTouchCancel];
    [upBlock_ release];
  }
  upBlock_ = [block copy];
  if(upBlock_) {
    [self addTarget:self action:@selector(fireUpBlock) forControlEvents:UIControlEventTouchUpInside];
    [self addTarget:self action:@selector(fireUpBlock) forControlEvents:UIControlEventTouchUpOutside];
    [self addTarget:self action:@selector(fireUpBlock) forControlEvents:UIControlEventTouchDragOutside];
    [self addTarget:self action:@selector(fireUpBlock) forControlEvents:UIControlEventTouchCancel];
  }
}


- (void (^)(void))clickedBlock { return clickedBlock_; }
- (void) fireClickedBlock { clickedBlock_(); }
- (void) setClickedBlock:(void (^)(void))block {
  if(clickedBlock_) {
    [self removeTarget:self action:@selector(fireClickedBlock) forControlEvents:UIControlEventTouchUpInside];
    [clickedBlock_ release];
  }
  clickedBlock_ = [block copy];
  if(clickedBlock_) {
    [self addTarget:self action:@selector(fireClickedBlock) forControlEvents:UIControlEventTouchUpInside];
  }
}

@end

答案 6 :(得分:1)

REKit带来阻止潜能。它使您能够使用Block。

向实例添加/覆盖方法

使用REKit,您可以动态制作一个目标 - 响应buttonAction - 如下所示:

id target;
target = [[NSObject alloc] init];
[target respondsToSelector:@selector(buttonAction) withKey:nil usingBlock:^(id receiver) {
    // Do something…
}];
[button addTarget:target action:@selector(buttonAction) forControlEvents:UIControlEventTouchUpInside];

您不需要创建子类或类别。

除了目标/行动范例,您还可以使用REKit进行委派模式。

答案 7 :(得分:1)

我发现使用一个小帮手类很容易和多才多艺:

@interface Handler : NSObject

@end

@implementation Handler {
    void (^block)(id);
}

+ (Handler *)create:(void (^)(id))block {
    Handler *result = [[Handler alloc] init];

    result->block = block;

    return result;
}

- (void)call:(id)sender {
    block(sender);
}

@end

并像这样使用它:

Handler *handler = [Handler create:^(id sender) {
    // ... handle the event, using local state captured by the block ...
}];

// store the handler because the target is not retained in addTarget
[handlers addObject:handler];

[button addTarget:handler action:@selector(call:) forControlEvents:UIControlEventTouchUpInside];

答案 8 :(得分:0)

我掀起的Swift扩展/基于类别的实现。使用OBJC关联对象不是反模式。 :P

 ' Start Excel and get Application object. 
        oXL = New Excel.Application

        ' Set some properties 
        oXL.Visible = False
        oXL.DisplayAlerts = False
        oXL.WindowState = Excel.XlWindowState.xlMaximized

        Dim wkbk As Excel.Workbook = oXL.Workbooks.Add

        'add(worksheet)

        wkbk.Worksheets.Add()

        'open existing work        

        oWB = oXL.Workbooks.Open("...")

        ' Get the active sheet 
        oSheet = DirectCast(oWB.ActiveSheet, Excel.Worksheet)
        oSheet.Name = "Sheet1"