私がこれを処理する方法は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
カスタムボタンクラスが本当に必要ない場合は、これを少し調整して、代わりにカテゴリを使用できると確信しています。