The other day I asked about using a UINavigationController as a child of a UIViewController. I got that w开发者_StackOverflow社区orking via the answer. Now what I'm trying to do is push a controller onto the nav stack. When a table cell is touched, I do the following:
- (void) showSetup {
NSLog(@"Showing Setup");
SetupViewController *controller = [[SetupViewController alloc]initWithNibName:@"SetupViewController" bundle:nil];
self.setupViewController = controller;
self.setupViewController.title = @"Setup";
[self.navigationController pushViewController:self.setupViewController animated:YES];
[controller release];
}
I can see the log statement in my console, but the view never changes. Am I missing something?
Hmmm, well it's a bit tricky without knowing the details of your implementation -- I assumed that you implemented your navigation controller as in the linked article. Also although you give no details it sounds like you've added a table view controller somewhere along the line, so I made the UIViewController conform to the UITableView protocols to handle everything in one place:
@interface SOViewController : UIViewController
<UITableViewDelegate,UITableViewDataSource > {
UINavigationController* navController; } - (IBAction) pushMe:(id)sender; @end
I dropped a button on the SOViewController's view in IB and wired the pushMe: action to it. I also created another UIViewController-based class called JunkController
and dropped a "Junk" label on it's view in IB -- that's all I did in IB. In the SOViewController's viewDidLoad:
navController = [[[UINavigationController alloc] init] retain]; navController.navigationBar.barStyle = UIBarStyleBlackOpaque; navController.toolbarHidden = YES; UITableViewController* tvController = [[UITableViewController alloc] init]; UITableView* tv = [[UITableView alloc] init]; tvController.tableView = tv; tv.delegate = self; tv.dataSource = self; [navController setViewControllers:[NSArray arrayWithObject:tvController]];
In the pushMe: action implementation:
[self presentModalViewController:navController animated:YES];
Implemented the tableView delegate and datasource methods; for selection:
- (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { NSLog(@"row selected"); JunkController* junk = [[JunkController alloc] initWithNibName:@"junkcontroller" bundle:nil]; [navController pushViewController:junk animated:YES]; [junk release]; }
This should yield an app that surfaces a screen with a "Push me" button. When that button is pressed you should get an animated modal navigation-based table view -- mine had one row in it that contained a label "select me". Touching this row should animate the junk controller into view.
There is no need to make setupViewController a declared property in this view controller. Also, I could be mistaken but I thought "controller" was a reserved name in Cocoa, I'd change that name. So make sure you have registered with the UITableViewDelegate and use - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
to hook into and push your new view controller as follows:
SetupViewController *detailViewController = [[SetupViewController alloc] initWithNibName:@"SetupViewController" bundle:nil];
detailViewController.title = @"Setup";
[self.navigationController pushViewController:detailViewController animated:YES];
[detailViewController release];
Goodluck!
精彩评论