Pergunta

So I have an IBAction hooked up to multiple buttons in IB I want this action to be fired when another IBAction is called I also want it to recognise an int from the second action as the [sender tag].

// viewController.m

-(IBAction)buttonPress {

 int getInter  = [sender tag]; 
 UIView *tmpView = [self.view viewWithTag:getInter];
 NSString *title = [(UIButton *)sender currentTitle]; 

}

I then have another action that needs to call buttonPress.

 -(IBAction)secondButtonPress {
  int varInt = 1
  [self buttonPress: nil] <--- How do I pass varInt that buttonPress would recognise as a sender Tag here?

 }

I realise that I could just create the button like so and duplicate the code in secondButtonPress but that seems to messy...

 UIButton *tmpButton2 = (UIButton *)[self.view viewWithTag:varInt];

So the question is, is there a way of tricking buttonPress into thinking it has been pressed and passing a variable through as the sender tag for that action. In a way programatically tricking buttonPress into thinking it has been pressed.

Thank you in advance.

Foi útil?

Solução

I ended up hooking it up to the same IBAction and running an if statement to do something if that tag was called.

Seems simple now!

-(IBAction)buttonPress {

 int getInter  = [sender tag]; 

   if ([sender tag] == 2) {
    //Do something just for that button.
   }

   else
   {
    UIView *tmpView = [self.view viewWithTag:getInter];
    NSString *title = [(UIButton *)sender currentTitle]; 
   }
}

Outras dicas

UIButton *btn1 = [[UIButton alloc] initWithFrame:CGRectMake(10, 10, 20, 20)];
[btn1 setTitle:@"Button 1" forState:UIControlStateNormal];
btn1.tag = 1;
[btn1 addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btn1];

UIButton *btn2 = [[UIButton alloc] initWithFrame:CGRectMake(10, 50, 20, 20)];
[btn2 setTitle:@"Button 2" forState:UIControlStateNormal];
btn2.tag = 2;
[btn2 addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btn2];

UIButton *btn3 = [[UIButton alloc] initWithFrame:CGRectMake(10, 90, 20, 20)];
[btn3 setTitle:@"Button 3" forState:UIControlStateNormal];
btn3.tag = 3;
[btn3 addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btn3];

write button click method as following and get button tag in it..

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

Just set the tag in initWithNibName method. Follow the sample and see whether it is helpful for you!!

@property(nonatomic,strong)IBOutlet UIButton *btn1;
@property(nonatomic,strong)IBOutlet UIButton *btn2;


- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {

       // Set the Tag
        self.btn1.tag=1;
        self.btn1.tag=2;

    }
    return self;
}

-(IBAction)selectedButtonClick:(id)sender {
    UIButton *btn = (UIButton *) sender;
    NSLog(@"selectedButton%d",btn.tag);
}
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top