0

I have a very simple application that has only 2 view controllers. The initial root controller has two UIButtons on it, and they both transition to the 2nd view controller via Storyboard. The information that is loaded on the 2nd view controller is determined by a buttonPressed IBAction that is linked to both the initial view controller. The problem I am having is that once I tap one of the two buttons, I get the 2nd view controller loading up blank. From debugging, I notice that the buttonPressed function is called after the 2nd view is loaded. How can I make it so that the buttonPressed function is called BEFORE the 2nd view controller is loaded blank?

Hardly any code involved, I have both UIButtons tagged 1 and 2, and the button press function loads two text files depending on the sender tag equaling 1 or 2. Everything is linked up in Storyboard. Any suggestions?

*buttonPressed function:

if([sender tag] == 1)

count = 1;

else

count = 2;

The count determines the file to be loaded.

*In Storyboard, I have just 2 view controllers, (No navigation or any of that) and two UIButtons on the initial view and a text view on the second to display text. The UIButtons are connected to buttonPressed and to the 2nd view controller segue.

4
  • Can you show the code for your buttonaction? Also, are you using segues to switch viewcontrollers? Do you have a UINavigationController?
    – Martol1ni
    May 16, 2012 at 19:43
  • I edited it, hope it makes more sense now. May 16, 2012 at 19:49
  • Does each button have its own segue or are you using the same segue for both? May 16, 2012 at 19:50
  • Separate segue for both buttons, but they are to the same place. May 16, 2012 at 19:51

2 Answers 2

1

Don't bother with a IBAction for button pressed. Have the buttons segue to the new controller and use prepareForSegue:sender: to figure out which button was tapped (the sender).

You can then get a reference to your controller that's about to appear with something like...

SecondController *next = (SecondController *)[segue destinationViewController];

...and tell it which file to load, which it can do in viewDidLoad:.

5
  • How is this a better solution than an IBAction? May 16, 2012 at 19:55
  • It's doesn't depend on the order of things being called and it takes advantage of the fact that you're using a storyboard. May 16, 2012 at 20:00
  • Well why would the order be wrong here? Why wouldn't the buttonPressed function be called immediately and then the view loaded? May 16, 2012 at 20:16
  • I'm not sure that it's wrong because I don't know anywhere that they define which is going to happen first. For example, I know that if the segue is from a table cell, the order is not what I would have expected. May 16, 2012 at 20:19
  • Well this is a test app that I made for my larger one. The buttonPressed loads immediately after pressed, then loads the information on another view. It works for 3 out of 5 buttons, yet the other 2 have the same issue I've described above. May 16, 2012 at 20:26
0

This sounds like a case for prepareForSegue!

-(void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if([[segue identifier] isEqualToString:@"First Button Segue Identifier"])
    {
        ViewController *theViewController = segue.destinationViewController;
        theViewController.textProperty = self.firstButtonText;
    } 
    else if ([[segue identifier] isEqualToString:@"Second Button Segue Identifier"])
    {
        ViewController *theViewController = segue.destinationViewController;
        theViewController.textProperty = self.secondButtonText;
    }
}

There you go, it is pretty generic because I didn't know your variable names or what text you are using. Similarly, if the segues don't have seperate identifiers, you can use the sender to determine which button is being pressed.

Good luck!

6
  • Could I implement the viewController in storyboard first and set the destination programatically in prepareForSegue? May 16, 2012 at 20:06
  • no, the destination is set in the storyboard. The UIStoryboardSegue has a property for "destinationViewController" which points to the view controller that you link to in your storyboard. So what we are doing here is snatching that viewController from the segue's property and assigning properties of that view with whatever information we want. You will need to replace ViewController with whatever you called your second view controller class, and textProperty with whatever property you are using to determine what text to display. May 16, 2012 at 20:13
  • if I override the prepareForSegue fuction, will I have to manage every segue in the entire project from now on? May 16, 2012 at 20:34
  • No you are not managing the segue. This function does nothing until you make it do something. The segue will transition on its own, this just lets you pause before the segue and "prepare" for the transition. The segue will still happen no matter what. You can have 15 different segues and only catch two of them in this function and actually do something and the other 13 will just keep trucking along like nothing happened. May 16, 2012 at 20:38
  • it worked! All I did was add my buttonPressed code to prepareForSegue. Not very efficient but it worked. Thanks for your help. May 16, 2012 at 20:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.