4

The title says most of what I'm looking for:

I have 2 buttons on my main menu that both call the same view controller. Depending on which button was clicked the view controller behaves a little differently. I thought I had the fix using NSNotificationCenter, but it won't catch anything the first time into the view controller (because it hasn't been loaded yet). Are there any other ways to do this?


EDIT: There seems to be some confusion, perhaps on my end. The problem is passing the information across multiple view controllers. The buttons in the Main Menu view controller CALL the second view controller, the problem is that second view controller not having any knowledge of any variables created in the Main Menu view controller.

ballofpopculture
  • 741
  • 1
  • 9
  • 18

4 Answers4

6

You could add a variable to the class of the second view controller and set that variable to a value depending on which button was pressed when you initialize the second view controller:

- (IBAction) buttonPressed:(id)button
{
    //Initialize your view controller
    MyViewController* secondViewController = [[MyViewController alloc] init...];

    //Assign a value to a variable you create (I called it pushedButtonValue) so the
    //viewController knows which button was pressed
    secondViewController.pushedButtonValue = [button tag];

    //Transition to the new view controller
    [self.navigationController pushViewController:secondViewController animated:YES];
}
Carter
  • 3,053
  • 1
  • 17
  • 22
2

The event handler for the button press will usually have an (id)sender parameter. Use this to determine which button was pressed based .

- (IBAction)pushButton:(id)sender {
    UIButton *button = (UIButton *)sender;
}
hspain
  • 17,528
  • 5
  • 19
  • 31
  • How does this work if (IBAction)pushButton:(id)sender is in ViewController1.m and it's ViewController2.m that cares which button was pressed? – ballofpopculture Aug 02 '11 at 17:51
  • if ([sender tag] == tagForFirstButton)... set the tag attribute on the button and you can retrieve it with [sender tag] in your event handler. – hspain Aug 02 '11 at 17:53
1

set up IBOutlets for each button and then test which sender is which button. But best way to do this it to have IBActions for each button to trigger, they then can call a method with a BOOL or enum telling the method how to behave, or do any of the different type of processing themselves and call a method that does the common code.

Nathan Day
  • 5,981
  • 2
  • 24
  • 40
0

Alternatively, you could set a tag for each button, preferably using a typedef or enum (for clarity). In the action method compare the value of the tag. You might have to typecast the sender object to a UIButton first.

See: How to set a UIButton tag with NSString?

Community
  • 1
  • 1
Wolfgang Schreurs
  • 11,779
  • 7
  • 51
  • 92
  • Does that work between ViewControllers? In this case the Main Menu is one controller, with two buttons (let's call them A and B). If I press A I want View Controller 2 to have a picture of a dog, and if I press B, I want View Controller 2 to have a picture of a cat. I think the big problem I'm running into here is pushing this information across view controllers. – ballofpopculture Aug 02 '11 at 17:54
  • You could add the typedefs or enums in a global header and import it in all files where you need these (probably the view controller and the menu). Then it should work fine. – Wolfgang Schreurs Aug 02 '11 at 17:56