简体   繁体   中英

UITableViewController isn't pushing views onto the stack

I have a UITableViewController that has a UISearchbar in its header view. When the user searches, my UITableView populates with the results. I want to push a new view controller onto the stack when the user selects a result.

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];

    if (cell.accessoryType == UITableViewCellAccessoryNone) {
        cell.accessoryType = UITableViewCellAccessoryCheckmark;

        WatchlistSheetView* watchlistSheetView = [[WatchlistSheetView alloc] initWithNibName:@"WatchlistSheetView" bundle:nil];
        watchlistSheetView.symbol = cell.textLabel.text;
        [self.navigationController pushViewController:watchlistSheetView animated:YES];
        [watchlistSheetView release];

        //[self dismissModalViewControllerAnimated:YES];
    }     
}

The following doesn't push a view controller onto the stack. Nothing happens. How can I resolve?

I guess that self.navigationController is nil, so that the message pushViewController is sent nowhere.

To fix this, you can create a UINavigationController in your application:didFinishLaunching and push your table view controller on to it as its root controller, along these lines:

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

    UINavigationController* navigation = [[UINavigationController alloc] init];

   ....
   [navigation pushViewController:yourTableViewController animated:NO];
   ...
   [window addSubview:[navigation view]];
   [self.window makeKeyAndVisible];
}

or you could use initWithRootController to initialize the navigation view controller with your table view controller.

It appears that you are calling self when referencing your navigation controller. Did you actually allocate a navigation controller and assign it to self?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM