Search code examples
iosuitableviewcocoa-touchuinavigationcontrollerpushviewcontroller

UINavigationController code push to UIViewController not working correctly


I am having an issue when I am pushing to a UIViewController by code from another UIViewController which has the same UINavigationController.

Notes & assumptions:

  1. I created all my UIViewControllers using IB but want to push between them using code.
  2. Both UIViewControllers contain UITableViews: The first to select search criteria & the second to display the results.
  3. "LT" stands for League Table in my class names.

I have three classes in my UINavigation Controller:

  1. LTMasterNavController - of type UINavigationController - owns the properties & results across all the subsequent view controllers. LTResultsViewController retrieved the results from here once LTOverviewViewController has generated them.
  2. LTOverviewViewController - of type UIViewController containing a UITableView. Used to select the filters to be displayed in LTResultsViewController.
  3. LTResultsViewController - of type UIViewControllercontaining a UITableView. Used to display the results from the LTOverviewViewController

Please find my code on the Dropbox link below: https://www.dropbox.com/sh/9m37y4f1gws99lf/VGNloaLSVO

When the "Show League Table" button is pushed in LTOverviewViewController I want the program to get the results from the filters & then immediately push to the LTResultsViewController.

With this code it pushes to the LTResultsViewController however the view appears to contain nothing. It is black with no UITable View & you cannot select anything.

In testing/debugging I created another button in LTOverviewViewController called "Push to Results" and connected it to LTResultsViewController as a push action in IB. It worked perfectly then when pushing the "Show League Table" button and then the "Push to Results" button, however I want it to happen without user having push that second button. Been stuck on this for days now so any help would be greatly appreciated!

Many thanks, James


Solution

  • With storyboards and segues this is fairly simple. In the storyboard, click on the connection between the UIViewController you want to transfer data between, I guess between LTOverviewViewController and LTResultsViewController, and give the segue an identifier name: say ResultsSegue.

    Now, in the LTOverViewController, you need to implement this method:

    - (void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
    

    In this method you will identify the segue name and the destinationViewController and pass the data you want the new view controller to have. Here is an example:

    - (void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
    {
        if ([[segue identifier] isEqualToString:@"ResultsSegue"])
        {
            LTResultsViewController *viewController = [segue destinationViewController];
            [viewController setData:self.data];
        }
    }
    

    When the storyboard wants to use this particular segue, this method will be called and executed before moving on to the new viewController.