受@locke 解决方案的启发,我将代码重写如下:
1- 在名为 preViewController 的 appdelegate 中定义一个 UIViewController 来保存主视图控制器。
2-要在视图控制器中引用它,请使用:
AppDelegate *appdelegate=(AppDelegate *)[[UIApplication sharedApplication] delegate];
3-编写以下masterviewcontroller如下:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
//create a mutable arry to hold the view controllers and copy the current list of navigation controllers
//at this point there is only the current view controller in stack
NSMutableArray *vcs = [NSMutableArray arrayWithArray:self.navigationController.viewControllers];
//create a detailed navigation controller
DetailedViewController *DVC = [[DetailedViewController alloc]initWithNibName:@"DetailedViewController" bundle:nil];
//create a shared variable enviroment to reference a global variable
AppDelegate *appdelegate=(AppDelegate *)[[UIApplication sharedApplication] delegate];
//assign the current viewcontroller to the temporary view controller
appdelegate.preViewController=[self.navigationController.viewControllers lastObject];
//insert detailed view into the array vcs before the current viewcontroller
if (![vcs containsObject:DVC]) {
[vcs insertObject:DVC atIndex:[vcs count]-1];
}
// update the self.navigation with the new stack
[self.navigationController setViewControllers:vcs animated:NO];
// pop the othernavigationcontroller from the navigation stack to fake a detailedview controller push
[self.navigationController popViewControllerAnimated:YES];
}
4 - 添加一个按钮来替换默认按钮并为详细视图控制器定义一个 IBaction (backClicked):
- (IBAction)backClicked:(id)sender{
AppDelegate *appdelegate=(AppDelegate *)[[UIApplication sharedApplication] delegate];
//push the holder view controller to fake a pop back to the master view controller
[self.navigationController pushViewController:appdelegate.preViewController animated:YES];
}