How to get tag number of UIButton

10,416

Solution 1

Try following code, it will surely help you out

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

Solution 2

You can get the tag with

sender.tag

Solution 3

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

}

Solution 4

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

Solution 5

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...
Share:
10,416
Rupesh
Author by

Rupesh

I have started my work as a Iphone Developer, I was not aware anything about Objective-C before i entered in this field. Now I am looking forward to set this as my career. Now I am keenly interested to grow my career in Mobile Application Development. And also interested in 2D 3D game development. Any help to grow my knowledge in this field will be highly appreciated.

Updated on June 04, 2022

Comments

  • Rupesh
    Rupesh almost 2 years

    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.

  • Rupesh
    Rupesh almost 13 years
    i have tried that but its showing error. following error "Request for member 'tag' in something not a structure or union"
  • EmptyStack
    EmptyStack almost 13 years
    Are you printing it in NSLog? tag is an integer property. You have to "%d" as the format to print integers.