Spawning a new instance of the same view, using a UINavigationController
I'm currently trying to spawn a new instance of the same view - using the following code:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
InventoryController *inventoryController = [[InventoryController alloc] initWithNibName:@"InventoryView" bundle:nil];
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:self];
[navigationController pushViewController:inventoryControlle开发者_JAVA百科r animated:YES];
[inventoryController release];
[navigationController release];
}
Problem is that it's not working...
I don't get any errors or anything - it just doesn't do anything.
Any ideas?
@PengOne has it right... you're creating a navigation controller and then releasing it, and there's nothing to prevent it from being deallocated. Additionally, you haven't added the nav controller's view to the window, and you haven't set the nav controller as the window's root view controller, so there's no way for the views controlled by controllers in this particular navigation stack to ever be seen.
Try this: Create a navigation-based project in Xcode. You don't need to add any code -- just create the project so that you can look at the code that's provided. You'll see that the app delegate has a retain property for storing the nav controller, and the nav controller is set as the window's root view controller.
If your current controller is already a part of UINavigationController hierarchy then you must not create a new navigation controller - use the existing one instead (note that every UIViewController has a reference to its parent UINavigationViewController if it exists):
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
InventoryController *inventoryController = [[InventoryController alloc] initWithNibName:@"InventoryView" bundle:nil];
[self.navigationController pushViewController:inventoryController animated:YES];
[inventoryController release];
}
精彩评论