2011-07-21 55 views
3

我正在使用各種按鈕,並且我已經從Xib文件中設置了它的標籤。並將所有按鈕連接到單個方法-(void) note:(id)sender如何獲取UIButton的標籤號碼

現在我想以檢索標籤number.so,我可以看到被點擊了哪個按鈕

-(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

嘗試下面的代碼,它一定會幫助你

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

Thanx它的工作。 – 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%@tagint

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...