13

のサブビューでイベントをトリガーしUITableViewCellて、レスポンダー チェーンをバブルアップさせ、カスタムUITableViewCellサブクラスで処理させようとしています。

基本的:

SomeView.m (のサブビューUITableViewCell)

[self.button addTarget:nil action:@selector(someAction:) events:UIControlEventTouchUpInside]

SomeCustomCell.m

- (void)someAction:(id)sender {
     NSLog(@"cool, the event bubbled up to the cell");
}

なぜこれが機能しないのかをテストするためsomeAction:に、ViewController にメソッドを追加しました。ViewController は、Cell が処理する必要があるにもかかわらず、テーブル ビュー セル サブビューから発生するイベントを最終的に処理するものです。セルがレスポンダー チェーン上にあることを確認し、セルの上下両方のレスポンダー チェーンのすべてのビューが、someAction:メソッドを実装する場合にイベントに応答することを確認しました。

ここで一体何が起こっているのですか?

これはそれを示すプロジェクトですhttps://github.com/keithnorm/ResponderChainTestこれはどういうわけか予想される動作ですか? UITableViewCell が他の UIResponder とは異なる方法で処理されていることを示すドキュメントは見つかりませんでした。

4

5 に答える 5

1

これはバグか、文書化されていない意図された動作であると結論付けました。いずれにせよ、サブビューでイベントに応答し、メッセージをレスポンダーチェーンに手動で伝播することで、力ずくで修正することになりました。何かのようなもの:

- (void)customEventFired:(id)sender {
  UIResponder *nextResponder = self.nextResponder;
  while (nextResponder) {
    if ([nextResponder respondsToSelector:@selector(customEventFired:)]) {
      [nextResponder performSelector:@selector(customEventFired:) withObject:sender];
      break;
    }
    nextResponder = nextResponder.nextResponder;
  }
}

この「修正」https://github.com/keithnorm/ResponderChainTestをどのように使用しているかを示すために、デモ プロジェクトも更新しました。

他の誰かがこれを理解しているなら、私はまだ他のアイデアを歓迎しますが、これは今のところ私が持っている最高のものです.

于 2014-01-24T22:03:57.347 に答える
0

このようにする

    @implementation ContentView

   // uncomment this to see event caught by the cell's subview

  - (id)initWithFrame:(CGRect)frame
 {
     self = [super initWithFrame:frame];
    if(self)
   {

    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    [button setTitle:@"Click" forState:UIControlStateNormal];
    [button setBackgroundColor:[UIColor blueColor]];
    [button addTarget:self action:@selector(customEventFired:) forControlEvents:UIControlEventTouchUpInside];
    button.frame = CGRectMake(4, 5, 100, 44);
    [self addSubview:button];
  }

    return self;
}

 - (void)customEventFired:(id)sender
{
     UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Event Triggered in cell subview" message:@"so far so good..." delegate:nil cancelButtonTitle:@"cancel" otherButtonTitles:nil, nil];
    [alertView show];
 }

@end

customEventFired:メソッドが呼び出されるようになりました

于 2014-01-24T07:58:29.013 に答える
0

View.m のコードを次のように変更できます。

      [button addTarget:nil action:@selector(customEventFired:) forControlEvents:(1 << 24)];

      [button addTarget:cell action:@selector(customEventFired:) forControlEvents:(1 << 24)];
于 2014-01-24T08:41:56.867 に答える
-1

これが最も簡単な解決策だと思います。ターゲットを指定しない場合、イベントは自動的にレスポンダー チェーンをバブルアップします。

[[UIApplication sharedApplication]sendAction:@selector(customAction:) to:nil from:self forEvent:UIEventTypeTouches];
于 2015-06-03T13:43:31.360 に答える