3

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.

0

5 Answers 5

13

Try following code, it will surely help you out

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

You can get the tag with

sender.tag
2
  • i have tried that but its showing error. following error "Request for member 'tag' in something not a structure or union"
    – Rupesh
    Jul 21, 2011 at 12:15
  • 1
    Are you printing it in NSLog? tag is an integer property. You have to "%d" as the format to print integers.
    – EmptyStack
    Jul 21, 2011 at 12:16
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];

}

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

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.