Search code examples
objective-ctagsibactionsender

Calling an IBAction in code and sending a tag through too


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.


Solution

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