我喜欢处理这个问题的方法是子类UIButton
化并添加一个基于块的操作:
@interface BlockButton : UIButton
@property (nonatomic, copy) void (^onPress)();
@end
@implementation BlockButton
-(id) initWithFrame:(CGRect)frame
{
if(self = [super initWithFrame:frame]) {
[self addTarget:self
action:@selector(pressed:)
forControlEvents:UIControlEventTouchUpInside];
}
return self;
}
-(void) pressed:(id)sender
{
if(self.onPress)self.onPress();
}
@end
然后代替
[dismissButton addTarget:self action:@selector(dismissViewControllerAnimated:YES completion:NULL) forControlEvents:UIControlEventTouchUpInside];
- (void)dismissThis
{
[self dismissViewControllerAnimated:YES completion:NULL];
}
您可以使用:
dismissButton.onPress = ^{
[self dismissViewControllerAnimated:YES completion:NULL];
};
UIButton
如果您真的不想要自定义按钮类,我相信您可以稍微调整一下以使用类别。