2

表示されたView Controllerを、ボタンから直接実行することで却下しようとしていますが、これを機能させる方法、またはそれが可能かどうかさえわかりません。

提供されるヘルプは大歓迎です!

私が試しているコード:

[dismissButton addTarget:self action:@selector(dismissViewControllerAnimated:YES completion:NULL) forControlEvents:UIControlEventTouchUpInside];

私がしたくないこと:

- (void)dismissThis
{
    [self dismissViewControllerAnimated:YES completion:NULL];
}
4

3 に答える 3

1

そのようには機能しません。UIControlsのドキュメントからaddTarget:action:forControlEvents::

アクション メッセージには、必要に応じて、送信者とイベントをパラメーターとしてこの順序で含めることができます。

したがって、3 つの可能なセレクターがあります。

@selector(name)
@selector(nameWithParam:)
@selector(nameWithParam: otherParam:)

セレクターの場合@selector(dismissViewControllerAnimated:completion:)、アニメーション化された BOOL の代わりに送信者で呼び出され、アプリをクラッシュさせる完了ハンドラー ブロックの代わりにイベントが呼び出されます。

クラッシュの理由を明確にするために編集し ます。メッセージdismissViewControllerAnimated:completion:を送信して完了ブロックをコピーしcopyます。イベント オブジェクトは実装されておらずcopyNSInvalidArgumentException.

于 2013-07-12T03:23:14.217 に答える
1

Apple の標準 API ではサポートされていませんが、UIControl のカテゴリを介してこの機能を簡単に追加できます。JTTargetActionBlockはこの機能を追加します。Cocoapod としても利用できます。

[button addEventHandler:^(UIButton *sender, UIEvent *event) {
    [self dismissViewControllerAnimated:YES completion:nil];
} forControlEvent:UIControlEventTouchUpInside];
于 2013-07-12T03:49:37.973 に答える
0

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

于 2013-07-12T03:52:52.560 に答える