2011-07-21 6 views
3

私は様々なボタンを使用しています。私はXibファイルからタグを設定しています。すべてのボタンを単一のメソッド-(void) note:(id)senderに接続しました。UIButtonのタグ番号を取得するには

は今、私がクリックされたボタンを参照することができ、タグnumber.soをretriveしたい

-(void) note:(id)sender 

{ 

    NotesClass *note = [[NotesClass alloc] initWithNibName:@"NotesClass" bundle:nil]; 
    note.notetag = sender; 
    NSLog(@"%@",note.notetag); 
    [self.navigationController pushViewController:note animated:YES]; 

} 

印刷がのNSLogが、私はこの出力を得ること:

<UIButton: 0x4e70350; frame = (227 119; 20 18); opaque = NO; autoresize = RM+BM; tag = 1; layer = <CALayer: 0x4e70480>> 

いずれかが私を助けてくださいすることができます。

答えて

12

次のコードを試してみて、それは確かにあなたを助けますtagとして%@int型である

UIButton *button = (UIButton *)sender; 
    NSInteger bTag = button.tag; 
+0

それはうまくいきました。 – Rupesh

1

あなたがして、タグを取得することができます

sender.tag 
+0

私はそのエラーを表示しようとしました。次のエラー "構造体または共用体でないメンバーのメンバー 'タグ'のリクエスト – Rupesh

+1

NSLogで印刷していますか? * tag *は整数プロパティです。整数を出力する形式として "%d"を使用する必要があります。 – EmptyStack

+0

私はそれを試してみます – Rupesh

1
(void) note:(id)sender 

{ 

    NotesClass *note = [[NotesClass alloc] initWithNibName:@"NotesClass" bundle:nil]; 
    note.notetag = [sender tag]; 
    NSLog(@"%d",note.notetag); 
    [self.navigationController pushViewController:note animated:YES]; 

} 
1
-(void) note:(id)sender 

{ 

NotesClass *note = [[NotesClass alloc] initWithNibName:@"NotesClass" bundle:nil]; 
note.notetag = [sender tag]; 
NSLog(@"%d",note.notetag); 
//Another option is to use 

UIButton *button = (UIButton *)sender; 
NSLog(@"%d",button.tag); 
[self.navigationController pushViewController:note animated:YES]; 

} 

その%dない

1
in .H file write below code 

@interface tagViewController : UIViewController { 

    UIButton *btn1; 
} 
@property(nonatomic,retain)IBOutlet UIButton *btn1; 
-(IBAction)btnclicked:(id)sender; 
@end 

and in .M file write below code 

-(IBAction)btnclicked:(id)sender 
{ 
    btn1 = (UIButton *)sender; 

    NSLog(@"You Press Button No %d",btn1.tag); 

} 

Don't forgate maping of your button Suppose i have three button and i set it tag 1,2,3 and then after mapping all of them with btnclicked: in TouchUp Inside Event and then after run it it's working... 
関連する問題