0

現在、ビューで UIButton オブジェクトを動的に作成しています。

それらに関する情報 (id - ラベル) を含む NSMutableArray があります。

次に、MutableArray で for イテレーションを実行してビュー オブジェクトを作成します。

ボタンでこのコードを使用して、タッチイベントをキャッチしようとしています:

[myButton addTarget:self action:@selector(selectedButton:) forControlEvents:UIControlEventTouchUpInside];

私のselectedButtonメソッドは正常に呼び出されましたが、ボタンが触れられたことを知ることはできません。

私はこれをやろうとしました:

-(void)selectedButton:(id)sender {...}

しかし、送信者オブジェクトをどうするかわかりません。

よろしくお願いいたします。

4

3 に答える 3

3

At the top of your .m file, put something like this:

enum {
    kButtonOne,
    kButtonTwo  
};

When you're creating your buttons, do this

myButton.tag = kButtonOne;

Then in your selected button method, do this:

-(void)selectedButton:(id)sender {
  switch (sender.tag) {
    case kButtonOne:
      // do something here
      break;
    case kButtonTwo:
      // do something else here
      break;
  }
}
于 2010-06-22T14:57:44.087 に答える
1

何かに設定mybutton.tagし、 でそのタグを確認しselectedButton:senderます。

于 2010-06-22T14:53:50.447 に答える
0
-(void)viewDidLoad{

UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];

button.frame = CGRectMake(80.0, 210.0, 40.0, 30.0);

button.tag=1;

[button addTarget:self 
           action:@selector(aMethod:)
 forControlEvents:UIControlEventTouchDown];

[button setTitle:@"raaz" forState:UIControlStateNormal];

[self.view addSubview:button];

}

-(IBAction)aMethod:(id)sender{

UIButton *btn=(UIButton *)sender;

NSLog(@"I have currently Pressed button=%d",btn.tag);


}
于 2010-06-22T15:05:47.283 に答える