views:

346

answers:

2

So I have an iPhone application that utilizes a UINavigationController for setting up the views. When the application first starts, it presents the user with a UITableViewController and the user can select an item and it will push another view. Now I have it set so that my app remembers the user's last selection and automatically selects it and loads the correct view controller. The only problem is that I am experiencing a really weird glitch when I load the next view automatically..

When the view is pushed, the navigation toolbar will change so that a back button directed to the previous view is showing but it won't display the next view. It will instead keep showing the table view and I can interact with it as well. I can press the back button and it will change the toolbar back and the tableview is still shown. Then when I select an item it loads the view just fine.

Thanks for the help.

Code:

I determining whether to push the view controller based on whether it can connect to a server. I do this in a backround thread:

- (void)startingThread
{    
    [NSThread detachNewThreadSelector:@selector(loginThread:) toTarget:self withObject:communicator];
}

- (void)loginThread:(MowerCommunicator *)communicator
{
    //If it can connect, launch thread complete.
    [self performSelectorOnMainThread:@selector(loginThreadComplete:) withObject:communicator waitUntilDone:NO];
}

- (void)loginThreadComplete:(MowerCommunicator *)communicator
{
    //push view controller
}

Now I have added NSLog statements to track if the view is actually "showing" and both viewWillAppear and viewDidAppear get called. I also check the delegate methods for the navigation controller and they get called as well.

I have a view that is the initial startup view and it reads from a server to determine what to display in the next table view. That gets pushed fine and when the tableview gets pushed, I hide the back button so the user can't get back to the first view without closing the app. Then the tableview looks at a variable in NSUserDefaults to determine with there is a saved index and then pushes the next view controller. That is when the glitch occurs. If I then press the back button to "go back" to the table view (this really just changes the navigation toolbar) and then I select an item from the table view, it loads the next view correctly. Also, I call the exact same methods when the user pressed an item from the table view and when the app loads the view automatically.

A: 

The simplest explanation is that you're pushing the initial tableviewcontroller onto the navigation controller's stack twice.

If you have the initial tableviewcontroller set as the nav's first controller in a nib but then you push the same controller onto the stack when trying to automatically display the stacked views, you would get what you are describing. The nav starts out with the controller from the nib and then you push another instance on top of that.

You should log the nav's viewControllers property before and after you do the automatic push to see what the actual state of the stack.

TechZen
Thanks for your response. I don't think I am pushing the view controller twice because I have a timer that gets activated when the subsequent view is pushed and that timer is getting activated.
I outputted the viewControllers stack and everything looks good. It shows the tableviewcontroller and then the other viewcontroller on top of it.
Does it show that the second time the mainController's view appears? If you have a navigation bar with a back button on it, the nav stack has to have at least two controllers on the stack. The navigation bar doesn't know how to display properly with a previous view controller to point to. You might want to post the code where you set up the automatic view hierarchy.
TechZen
See edit above.
A: 

Did you ever find out what's wrong? I think I'm having the same problem. In my case I also notice that the property self.navigationController is nil in the subcontroller (the one that got pushed the last).

Stefan
Sorry I never did resolve this issue. Instead of pushing the controllers on automatically, I just didn't do it at startup. Are you still experiencing this issue in iOS 4? I am hoping that this is resolved in that version.
Same problem in iOS 4. I think I must be doing something wrong though, because when I replace my custom UIViewController subclasses (the ones I'm pushing onto the navigation stack) with standard UITableViewControllers, then everything works fine. Even when I replace only one of the two (either one), it still works fine...
Stefan
Just found my problem. It took me many, many hours to find. In all the views of the navigationcontrollers in my tab bar controller, I have a 'popViewController'. This causes the navigation controller to return to the root controller when you switch tabs. I never realized that viewWillDisappear (and hence popViewController) was also called when going into the detail controller. So removing that call fixed it.
Stefan