How show tag of button?

2019-08-22 17:23发布

问题:

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

}

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

}} 

回答1:

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);
} 


回答2:

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

    }
} 


回答3:

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



回答4:

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



回答5:

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)
}