How show tag of button?

In below code i want display tag of each button on console. I tried for that but it show out of scope. How do that?

 - (void)loadView {
[super loadView];
self.view.backgroundColor = [UIColor redColor];
UIScrollView *scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
scroll.pagingEnabled = YES;
NSInteger numberOfViews = 33;
[btnMenu setTag:0 ];
for (int i = 1; i < numberOfViews; i++) {
    CGFloat yOrigin = i * self.view.frame.size.width;
    UIView *awesomeView = [[UIView alloc] initWithFrame:CGRectMake(yOrigin, 0, self.view.frame.size.width, self.view.frame.size.height)];
    //awesomeView.backgroundColor = [UIColor colorWithRed:0.5/i green:0.5 blue:0.5 alpha:1];
    btnMenu = [UIButton buttonWithType:UIButtonTypeCustom];
    //NSData *data =UIImageJPEGRepresentation(, 1);
    [btnMenu setBackgroundImage:[UIImage imageNamed:[NSString stringWithFormat:@"page-%d.jpg",i]] forState:UIControlStateNormal];
    CGRect frame = btnMenu.frame;
    frame.size.width=320;
    frame.size.height=460;
    frame.origin.x=0;
    frame.origin.y=0;
    btnMenu.frame=frame;
    [btnMenu setTag:i];
    btnMenu.alpha = 1;
    [btnMenu addTarget:self action:@selector(btnSelected:) forControlEvents:UIControlEventTouchUpInside];
    [awesomeView addSubview:btnMenu];

    [scroll addSubview:awesomeView];
    [awesomeView release];
}
scroll.contentSize = CGSizeMake(self.view.frame.size.width * numberOfViews, self.view.frame.size.height);
[self.view addSubview:scroll];
[scroll release];

}

  • CGRectMake is not working with UIView
  • Same code base for iPhone/Desktop app
  • How to enable Neon instruction in Xcode
  • Recording from Built-In Mic when Playing through Bluetooth in iOS
  • iOS 5 does not allow to store downloaded data in Documents directory?
  • UIKeyboardTypeNumberPad without a done button
  • -(IBAction)btnSelected:(id)sender{
    switch (btnMenu.tag) {
            NSLog(@"%d",btnMenu.tag);
    
    }} 
    

    5 Solutions Collect From Internet About “How show tag of button?”

    try this:

    -(IBAction)btnSelected:(id)sender{
      UIButton *button = (UIButton *)sender;
      int whichButton = button.tag;
      NSLog(@"Current TAG: %i", whichButton);
    } 
    

    EDIT:

    do you really need the method as an IBAction method?

    cannot you use it as a void one?

    -(void)btnSelected:(id)sender{
      UIButton *button = (UIButton *)sender;
      int whichButton = button.tag;
      NSLog(@"Current TAG: %i", whichButton);
    } 
    
    -(IBAction)btnSelected:(id)sender{
        UIButton* btnMenu = (UIButton*)sender;
        switch (btnMenu.tag) {
        NSLog(@"%d",btnMenu.tag);
    
        }
    } 
    

    try this code as your action method

    -(IBAction)btnSelected:(id)sender{
    switch (sender.tag) {
        NSLog(@"%d",sender.tag);
    

    }}

    this prints the tag of the current button that is selected .

    TNQ

    -(IBAction)btnSelected:(id)sender{
    
    UIButton *btnSelected = sender;
    
    NSLog(@"%d",btnSelected.tag);
    }
    

    Above code is working for me and it is printing tag value in log. You must did something wrong.

    While creating your button create it locally (inside for loop), otherwise it will contain only last tag.

    totalNoOfImages=32;
    

    as:

    for(int i=0;i<totalNoOfImages;i++)
    {   
        UIButton *button=[[[UIButton alloc] initWithFrame:CGRectMake(i*50,0,45,44)] autorelease];       
    
        //SETTING TAG FOR IMAGE
        [button setTag:i];
        [button addTarget:self action:@selector(btnSelected:)forControlEvents:UIControlEventTouchDown];
        [scrollView addSubview:iconImageSlide];
    }   
    

    Following method will show the tag:

    - (void) btnSelected:(id)sender
    {
        int btnId=[(UIButton *)sender tag];
        NSLog(@"btnTag= %d", ibtnId)
    }