Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/111.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 有没有办法在自定义UIButton中调用TouchUpInside操作?_Ios_Objective C_Uibutton - Fatal编程技术网

Ios 有没有办法在自定义UIButton中调用TouchUpInside操作?

Ios 有没有办法在自定义UIButton中调用TouchUpInside操作?,ios,objective-c,uibutton,Ios,Objective C,Uibutton,我正在为我的应用程序编写自定义ui按钮。但是,我想为按钮添加一个完整的操作。这样,我可以从操作中返回BOOL,然后在按钮中执行一些代码(即显示动画),然后调用完成方法 因此,理想情况下,我希望能够做到以下几点: [button addAction:^(){ NSLog(@"Action!"); return true; } completion:^() { NSLog(@"Completion!"); return true; } forControlEvents

我正在为我的应用程序编写自定义
ui按钮。但是,我想为按钮添加一个完整的操作。这样,我可以从操作中返回
BOOL
,然后在按钮中执行一些代码(即显示动画),然后调用完成方法

因此,理想情况下,我希望能够做到以下几点:

[button addAction:^(){
    NSLog(@"Action!");
    return true;
} completion:^() {
    NSLog(@"Completion!");
    return true;
} forControlEvents:UIControlEventTouchUpInside];

如何覆盖发生
UIControlEventTouchUpInside
时发生的情况?或者另一个控制事件。

您可以通过执行以下操作来实现这一点:

自定义按钮

@interface CustomButton : UIButton
- (void)addAction:(void (^)(CustomButton *button))action onCompletion:(void (^)(CustomButton *button))completion forControlEvents:(UIControlEvents)event;
@end
自定义按钮

#import "CustomButton.h"

@interface CustomButton ()

@property (nonatomic, copy) void(^actionHandler)(CustomButton *button);
@property (nonatomic, copy) void(^completionHandler)(CustomButton *button);

@end

@implementation CustomButton

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/



- (void)addAction:(void (^)(CustomButton *button))action onCompletion:(void (^)(CustomButton *button))completion forControlEvents:(UIControlEvents)event
{
    self.actionHandler = action;
    self.completionHandler = completion;

    __weak __typeof__(self) weakSelf = self;
    [self addTarget:weakSelf action:@selector(buttonAction) forControlEvents:event];
}

- (void)buttonAction
{
    if (self.actionHandler) {
        self.actionHandler(self);
    }

    // This will execute right after executing action handler.
    // NOTE: If action handler is dispatching task, then execution of completionHandler will not wait for completion of dispatched task
    //       that should handled using some notification/kvo.
    if (self.completionHandler) {
        self.completionHandler(self);
    }
}


@end

您可以通过执行以下操作来实现这一点:

自定义按钮

@interface CustomButton : UIButton
- (void)addAction:(void (^)(CustomButton *button))action onCompletion:(void (^)(CustomButton *button))completion forControlEvents:(UIControlEvents)event;
@end
自定义按钮

#import "CustomButton.h"

@interface CustomButton ()

@property (nonatomic, copy) void(^actionHandler)(CustomButton *button);
@property (nonatomic, copy) void(^completionHandler)(CustomButton *button);

@end

@implementation CustomButton

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/



- (void)addAction:(void (^)(CustomButton *button))action onCompletion:(void (^)(CustomButton *button))completion forControlEvents:(UIControlEvents)event
{
    self.actionHandler = action;
    self.completionHandler = completion;

    __weak __typeof__(self) weakSelf = self;
    [self addTarget:weakSelf action:@selector(buttonAction) forControlEvents:event];
}

- (void)buttonAction
{
    if (self.actionHandler) {
        self.actionHandler(self);
    }

    // This will execute right after executing action handler.
    // NOTE: If action handler is dispatching task, then execution of completionHandler will not wait for completion of dispatched task
    //       that should handled using some notification/kvo.
    if (self.completionHandler) {
        self.completionHandler(self);
    }
}


@end

您需要的信息在
UIControl
参考/编程指南中。它不特定于
ui按钮
。苹果的搜索现在很糟糕,所以我无法快速找到/粘贴任何内容,但这将为您指明正确的方向。您需要的信息在
UIControl
参考/编程指南中。它不特定于
ui按钮
。苹果的搜索现在糟透了,所以我无法快速找到/粘贴任何内容,但这将为您指明正确的方向。