0

ネストされたを含むViewControllerがありますUIView。追加するサブビューごとにタップリスナーを追加したいと思います。しかしSIGABRT、サブビューをタップするとエラーが発生しました。

これが私のコードです:

- (void) viewDidLoad {
    [super viewDidLoad];

    //set container
    UIView *container = [[UIView alloc] initWithFrame:CGRectMake(5, 5, 320, 420)];
    container.backgroundColor = [UIColor blueColor];

    //create new subview within container. I call it "card"
    for (int i=0; i<5; i++)
    {
        //create card
        UIView *card = [[UIView alloc] initWithFrame:CGRectMake(10, 35 + (65 * i), 300, 45)];
        card.backgroundColor = [UIColor greenColor];

        //set tap event with action selector = cardRowTapped
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(cardRowTapped)];
        [card addGestureRecognizer:tap];

        //add subview
        [container addSubview:card];
    }

    //add subview to self
    [self.view addSubview:container];

}

これが私のタップハンドラーコードです

- (void) cardRowTapped:(UITapGestureRecognizer *)gr {
    NSLog( @"hello");
}

コンソール出力:

2012-10-03 13:23:37.173 MyProject [5167:707]-[MyViewController cardRowTapped]:認識されないセレクターがインスタンス0x2cd810に送信されました

2012-10-03 13:23:37.179 MyProject [5167:707] *キャッチされなかった例外'NSInvalidArgumentException'が原因でアプリを終了しています、理由:'-[MyViewController cardRowTapped]:認識されないセレクターがインスタンス0x2cd810に送信されました

*まずスローコールスタック:例外(lldbを)投げる(0x314b888f 0x377f6259 0x314bba9b 0x314ba915 0x31415650 0x30c45637 0x30bd5d65 0x30e06479 0x30b51f55 0x30b50aa3 0x30b5d7e9 0x30b5d627 0x30b5d1f5 0x30b43695 0x30b42f3b 0x3348922b 0x3148c523 0x3148c4c5 0x3148b313 0x3140e4a5 0x3140e36d 0x33488439 0x30b71cd5 0x76e8d 0x76e28)と呼ばれるTERMINATE

なぜこれが起こっているのか考えていますか?

4

3 に答える 3

0

メソッドにはジェスチャによって渡されるメンバー変数が必要なので、以下のようにタップジェスチャメソッドを変更します。

UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(cardRowTapped:)];

ARCを使用していない場合は、もう1つ、機能するはずです。使用後にオブジェクトを解放します。

于 2012-10-03T06:22:09.110 に答える
0

ジェスチャを追加するときにこのコードを試して、次のようなデリゲートを指定してください..

UIView *card = [[UIView alloc] initWithFrame:CGRectMake(10, 35 + (65 * i), 300, 45)];
        card.backgroundColor = [UIColor greenColor];

        //set tap event with action selector = cardRowTapped
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(cardRowTapped:)];
        [tap setNumberOfTapsRequired:1];
        [tap setDelegate:self];
        [card addGestureRecognizer:tap];
        [tap release];
        //add subview
        [container addSubview:card];

次のような .h ファイルでデリゲートを指定します。

@interface ViewController : UIViewController<
UIGestureRecognizerDelegate>{
//.....
}

これがお役に立てば幸いです...

:)

于 2012-10-03T06:23:14.350 に答える
0

このコードを変更するだけです:

//set tap event with action selector = cardRowTapped
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(cardRowTapped:)];
        [card addGestureRecognizer:tap];

唯一の違いは次のとおりです。

@selector(cardRowTapped:)
if you have the method with parameter then ":" should be there.
于 2012-10-03T06:31:37.057 に答える