Search code examples
iphoneobjective-cios6

how to activate a button when another button is clicked in Obj C equivalent to java .doClick()


Can someone please elaborate on how to use this [button sendActionsForControlEvents:UIControlEventTouchUpInside]; to click a button from within a different button click.

  .m
  int i = 0;

- (IBAction)brain:(UIButton *)sender {
   //Brain of the operation
    i++;
   }


- (IBAction)subBrain:(UIButton *)sender {
  if(i > 1){
  /* Here if the brain had been prior clicked then when subBrain is 
  clicked, edit some variables and re-click the brain button 
  how would I go about this?!? Also is there a better way of doing this?*/
  [button sendActionsForControlEvents:UIControlEventTouchUpInside];
  }

  }

   .h
- (IBAction)brain:(UIButton *)sender;
- (IBAction)subBrain:(UIButton *)sender;

Solution

  • In iOS, you would usually not try to trigger the other Button in the UI, but trigger the function directly that the Button is attached to. So in your case, you would call:

    [self brain:nil]
    

    Or better yet, drag the button itself to the .h file and create a new referencing outlet, calling it "brainButton" or something. Than you can refer to "self.brainButton" in the code, and could do something like

    [self brain:self.brainButton] // if you need the sender