pushViewController not working with tab bar controller

user973985 picture user973985 · Dec 17, 2011 · Viewed 7.5k times · Source

I am trying to push in a new view controller with the code:

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

But its not working. This code happens when you select a cell in my table view. I have a table view which I think is preventing this from happening. I have tried presenting it modally but that gets rid of the navigation bar and i can't go back. There is an extremely similar to this but the answer didn't work for me!

UPDATE

- (void)loadView {
    // Create an instance of UIWebView as large as the screen
    CGRect screenFrame = [[UIScreen mainScreen] applicationFrame];
    UIWebView *wv = [[UIWebView alloc] initWithFrame:screenFrame];
    // Tell web view to scale web content to fit within bounds of webview 
    [wv setScalesPageToFit:YES];

    [self setView:wv];
    [wv release];
}

puts the web view as the WebViewController's view

there is no nib as shown above

the didSelect cell method IS CALLED using an NSLog to find out

Everything is initialized and allocated and non-zero

UPDATE:

my did select cell method:

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    // Push the web view controller onto the navigaton stack - this implicity
    // creates the web view controller's view the first time through

    NSLog(@"TOUCHED!");

    webViewController = [[WebViewController alloc] init];

    if (webViewController == nil) {
        NSLog(@"webViewController in nil state");
    }

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


    // Grab the selected item
    RSSItem *entry = [[channel items] objectAtIndex:[indexPath row]];

    // Construct a URL with the link string of the item
    NSURL *url = [NSURL URLWithString:[entry link]];

    // Construct a request object with that URL
    NSURLRequest *req = [NSURLRequest requestWithURL:url];

    // Load the request into the web view
    [[webViewController webView] loadRequest:req];

    // Set the title of the web view controller's navigation item
    [[webViewController navigationItem] setTitle:[entry title]];

}

ALL OF THIS WORKED BEFORE INSERTING TAB BAR CONTROLLER

UPDATE

Where I create the controllers (IN APP DELEGATE):

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions

             self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
    // Override point for customization after application launch.
    ListViewController *lvc = [[ListViewController alloc] initWithStyle:UITableViewStylePlain];

    [lvc autorelease];
    // Create the tabBarController
    UITabBarController *tabBarController = [[UITabBarController alloc] init];

    // Create two view controllers
    UIViewController *vc1 = [[ListViewController alloc] initWithStyle:UITableViewStyleGrouped];
    UIViewController *vc2 = [[YoutubeViewController alloc] init];

    // Make an array containing the two view controllers
    NSArray *viewControllers = [NSArray arrayWithObjects:vc1, vc2, nil];

    // The viewControllers array retains vc1 and vc2, we can release
    // our ownership of them in this method
    [vc1 release];
    [vc2 release];

    // Attach them to the tab bar controller
    [tabBarController setViewControllers:viewControllers];

    // Put the tabBarController's view on the window
    [[self window] setRootViewController:tabBarController];

    // The window retains tabBarController, we can release our reference
    [tabBarController release];

    // Show the window
    [[self window] makeKeyAndVisible];

    return YES;
}

So I really have no idea whats going on here. Also IF YOU COULD PLEASE HELP ME WITH ANOTHER QUESTION! Go to my profile and see the question about how to stop cutoffs/add extra line for text label in uitableviewcell

Answer

Ken W picture Ken W · Dec 17, 2011

I don't see the initial UINavigationViewController in your app delegate. In order to use the self.navigationController pushViewController, the view controller needs to be in one of the self.navigationController.viewControllers. Can you modify your code and try the following and see it works for you.

// Create two view controllers
UIViewController *vc1 = [[ListViewController alloc] initWithStyle:UITableViewStyleGrouped];
UIViewController *vc2 = [[YoutubeViewController alloc] init];

// Create the UINavigationController and put the list view controller as the root view controller
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:vc1];

// Make an array containing the two view controllers and the UINavigationController which has the ListViewController is the first one.
NSArray *viewControllers = [NSArray arrayWithObjects:navController, vc2, nil];

// The viewControllers array retains vc1 and vc2, we can release
// our ownership of them in this method
[vc1 release];
[vc2 release];
[navController release];

// Attach them to the tab bar controller
[tabBarController setViewControllers:viewControllers];