Alex Salom
Alex Salom

Reputation: 3094

Pushing a UIViewController onto a UINavigationController (which also inside a UITabBarController)

I got a UITabBarController created programmatically in my AppDelegate. I also created two UINavigationController with some UIViewControllers inside them. I added the navigation controller onto the UITabBarController, like this:

    BVMapViewController *mapViewController = [[[BVMapViewController alloc] initWithNibName:@"BVMapViewController_iPhone" bundle:nil] autorelease];
    BVFavoritesViewController *favoritesViewController = [[[BVFavoritesViewController alloc] initWithNibName:@"BVFavoritesViewController_iPhone" bundle:nil] autorelease];

    UINavigationController *navigationController = [[[UINavigationController alloc] initWithRootViewController:mapViewController] autorelease];
    [navigationController setToolbarHidden:YES];
    UINavigationController *navigationControllerForFavorites = [[[UINavigationController alloc] initWithRootViewController:favoritesViewController] autorelease];

    self.tabBarController = [[[UITabBarController alloc] init] autorelease];
    NSArray* controllers = [NSArray arrayWithObjects:navigationController, navigationControllerForFavorites, nil];
    self.tabBarController.viewControllers = controllers;
    [self.window setRootViewController:self.tabBarController];

The result works properly creating a tab bar controller with two navigation controllers.

The problem is as follows: The first UINavigationController contains a UIViewController with a MKMapView inside it. That map shows a bunch of POIs so when you tap on one of them, the app sends a request to a webservice (in the background) which as soon as it finishes it informs the view controller which pushes another UIViewController into the navigation stack to show some more information about that POI,

[self.navigationController pushViewController:self.stationViewController
                                     animated:YES];

Here I get the following error:

_WebTryThreadLock(bool), 0x11d9b560: Tried to obtain the web lock from a thread other than the main thread or the web thread. This may be a result of calling to UIKit from a secondary thread. Crashing now... 1 0x58a76a9 WebThreadLock 2 0x5bfdfe -[UITextView _updateForNewSize:withOldSize:] 3 0x5c00b4 -[UITextView setFrame:] 4 0x4c59df UIViewCommonInitWithFrame 5 0x4c5bae -[UIView initWithCoder:] 6 0x4d9fb7 -[UIScrollView initWithCoder:] 7 0x5bc851 -[UITextView initWithCoder:] 8 0x7b1a02 UINibDecoderDecodeObjectForValue 9 0x7b10e5 -[UINibDecoder decodeObjectForKey:] 10 0x6a2648 -[UIRuntimeConnection initWithCoder:] 11 0x7b1a02 UINibDecoderDecodeObjectForValue 12 0x7b1418 UINibDecoderDecodeObjectForValue 13 0x7b10e5 -[UINibDecoder decodeObjectForKey:] 14 0x6a1aa3 -[UINib instantiateWithOwner:options:] 15 0x566e37 -[UIViewController _loadViewFromNibNamed:bundle:] 16 0x567418 -[UIViewController loadView] 17 0x567648 -[UIViewController loadViewIfRequired] 18 0x567882 -[UIViewController view] 19 0x567b2a -[UIViewController contentScrollView] 20 0x57eef5 -[UINavigationController _computeAndApplyScrollContentInsetDeltaForViewController:] 21 0x57efdb -[UINavigationController _layoutViewController:] 22 0x57f286 -[UINavigationController _updateScrollViewFromViewController:toViewController:] 23 0x57f381 -[UINavigationController _startTransition:fromViewController:toViewController:] 24 0x57feab -[UINavigationController startDeferredTransitionIfNeeded:] 25 0x5804a3 -[UINavigationController pushViewController:transition:forceImmediate:] 26 0x580098 -[UINavigationController pushViewController:animated:] 27 0x386f -[BVMapViewController parserDidFinishWithStation:] 28 0x785c -[BVParser parseStation:] 29 0xf8a0d5 -[NSThread main] 30 0xf8a034 NSThread_main 31 0x94e53ed9 _pthread_start

I tried to perform this on the main thread but the pushed controller gets its view all black.

Thank you in advance and have a good day.

Upvotes: 0

Views: 902

Answers (1)

lnafziger
lnafziger

Reputation: 25740

Well, as you alluded to in your question, you must push the view controller on the main thread. Any interaction with UIKit must be done on the main thread.

As to why it shows up black, that is a different issue which you need to resolve by standard troubleshooting techniques. Once it is on the main thread and it is running correctly, set breakpoints on the various methods of the class being displayed (viewDidLoad, etc) and see where it is tripping up.

Upvotes: 1

Related Questions