Rupesh
Rupesh

Reputation: 2051

How to get tag number of UIButton

I am using Various button, and i have set tag of it from Xib files. And Connected all button to single method -(void) note:(id)sender.

Now i want to retrive tag number.so that i can see which button is clicked

-(void) note:(id)sender

{

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

}

When Print that NSlog I get this output:

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

Any one can please help me.

Upvotes: 3

Views: 10495

Answers (5)

Try following code, it will surely help you out

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

Upvotes: 13

dipen chudasama
dipen chudasama

Reputation: 26

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

Upvotes: 1

Maulik
Maulik

Reputation: 19418

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

}

Upvotes: 1

visakh7
visakh7

Reputation: 26390

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

}

Its %d not %@ as tag is of int type

Upvotes: 1

Sascha
Sascha

Reputation: 5973

You can get the tag with

sender.tag

Upvotes: 1

Related Questions