Crystal
Crystal

Reputation: 29458

UINavigationController visibleViewControllers

I have a UITabBarController, and one tab is a UINavigationController. I have a search bar that goes to a certain view within the UINavigationController. The problem is that if the first view is not pushed by the UINavigationController, than it crashes because my search doesn't recognize the visibleViewController from this call:

UINavigationController *navController = [self.MainTab.viewControllers objectAtIndex:1];
FirstViewController *fVC = [navController visibleViewController];

What I don't understand is, before this code, I do this:

    self.MainTab.selectedIndex = 1;

This code on its own selects the viewController in that tab, where then the view gets loaded to my knowledge. So shouldn't this be enough for the [navController visibleViewController] to get the current viewController? Thanks.

Upvotes: 2

Views: 9891

Answers (3)

Chris
Chris

Reputation: 953

I know this has been answered, but I found another solution that might be helpful. In my case I was handling rotation differently for some viewControllers within my NavigationController, I did the following:

Subclass UINavigationController, then where needed in your new subclass you can access the current visibleViewController's title like so:

- (BOOL)shouldAutorotate
{
    if ([[self visibleViewController].title isEqualToString:@"Special Case"]) {
        return NO;
    }
    return YES;
}

This is not specific to rotation, this is just what I used it for. The only thing you have to do is set your self.title for each of the viewControllers you are checking against in their viewDidLoad, if they are set in IB or are not set they will be nil.

Upvotes: 0

sergio
sergio

Reputation: 69027

From what you explain in your question and comments, I understand that your code tries to access an object of type FirstViewController, supposedly the first view to be pushed on to your UINavigationController, when it has not yet been created.

On the other hand, if you first programmatically select the tab, the view is created and everything works fine. Indeed, that view is created in a viewDidLoad method that is run when the tab is selected.

The solution I would suggest is avoiding accessing the UINavigationController visibleViewController directly from your search tab; instead, let your search code access the model (as in Model-View-Controller) for your app and store there the result; then, from the mentioned viewDidLoad method again access the model to read the search result and update/show the UI.

This is the clean solution, IMO. If you want a sort of workaround to your current design, then check the fVC value you get back from visibleViewController and if it is not what expected, then instantiate the view properly.

I hope this helps.

Upvotes: 1

EmptyStack
EmptyStack

Reputation: 51374

Try topViewController instead of visibleViewController.

FirstViewController *fVC = [navController topViewController];

Upvotes: 5

Related Questions