1

I have two buttons on top of each other in my nib. I need both of them to be pressed when tapped, but only the top button carries out its function. Is there a way for the top button to tell the bottom button to activate when the top one gets pressed. I do not think I can just merge the buttons into one and use one -(IBAction)buttonName function because one button is bigger than the other so I do not always need both to activate when one of them is pressed.

Thanks!

Gabriele Petronella
  • 106,943
  • 21
  • 217
  • 235
Greg M
  • 27
  • 6
  • 4
    First off, why do you have two buttons on top of each other? – John Parker Aug 07 '13 at 21:14
  • 2
    That doesn't answer the "Why?". – Jon Shier Aug 07 '13 at 21:18
  • Yes, but *why*? (What effect are you trying to achieve?) I'd be surprised if there maybe wasn't a neater solution than stacking buttons, although @Caleb's answer will solve your problem. – John Parker Aug 07 '13 at 21:18
  • the bottom button is the whole screen which changes what is displayed. the top button plays a sound, except there are 4 top buttons that plays different sounds – Greg M Aug 07 '13 at 21:20
  • 1
    Making the whole screen a button is a bit odd (you could just capture the taps via `touchesBegan:...` etc. on the view itself for example), but I suppose if it works, it works. – John Parker Aug 07 '13 at 21:23

2 Answers2

3

Is there a way for the top button to tell the bottom button to activate when the top one gets pressed.

Not really, but you can have the action for the top button call the action for the bottom button.

Here's one way:

- (IBAction)actionTop:(id)sender
{
    NSLog(@"The top button was activated.");
    [self actionBottom:self];
}

- (IBAction)actionBottom:(id)sender
{
    NSLog(@"The bottom button was activated.");
}

Another way would be to use the same action for both, and figure out what to do based on which button triggered the action:

- (IBAction)action:(id)sender
{
    // if the top button was tapped, do this part
    if (sender == self.topButton) {
        NSLog(@"The top button was activated.");
    }

    // you want the bottom button to be activated no matter which button was tapped, so
    // no need to check here...
    NSLog(@"The bottom button was activated.");
}

the bottom button is the whole screen which changes what is displayed. the top button plays a sound, except there are 4 top buttons that plays different sounds

It seems like an invisible button covering the whole screen might be the wrong way to tackle the problem. You might look into using a gesture recognizer attached to your view to trigger the change. Your button actions could call the same method that the gesture recognizer uses.

Caleb
  • 124,013
  • 19
  • 183
  • 272
  • dont you need an if statement somewhere so that the bottom knows when to do its function – Greg M Aug 07 '13 at 21:18
  • @GregM If you're using my first example, you'd just set `actionTop:` as the top button's action, and `actionBottom:` as the bottom button's action. If the top button is tapped, it's action does whatever it needs to do and then triggers the bottom action. If the bottom button is tapped, it just triggers the bottom action. – Caleb Aug 07 '13 at 21:22
  • I was not using (id)sender before, I just had -(IBAction)actionTop, does that make a difference? – Greg M Aug 07 '13 at 21:29
  • The `sender` parameter is optional. Doesn't make a difference if you don't need `sender`, but if you do (as in my second example) you can include it. – Caleb Aug 07 '13 at 21:33
  • sorry I'm bad at this, for the top example it says no visible @interface declares the selector "actionBottom" – Greg M Aug 07 '13 at 21:42
  • Right, you'll need to add declarations for those methods to your .h file too. Or, you can just reverse the order so that `-actionBottom:` appears before `-actionTop:`. – Caleb Aug 07 '13 at 21:53
2

Since one button is larger than the other, I assume that you would like the smaller button to "bring down" the larger button with it, including the change in the visual state. In cases like that you could send your target button a "tap" programmatically, like this:

- (IBAction)largeButtonClick:(id)sender {
}

- (IBAction)smallButtonClick:(id)sender {
    // Perform the acton specific to only the small button, then call
    [largeButton sendActionsForControlEvents:UIControlEventTouchUpInside];
}
Community
  • 1
  • 1
Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523