如何在iOS应用程序中手动设置“返回”目的地

我有一个名为“detailViewController”的UIViewController。

这个视图控制器是通过使用push segue的多个不同阶段来访问的。

我面临的问题是,detailViewController上的后退button将用户带回到以前的视图控制器(因为它应该),但是我想要后退button将用户带到masterViewController(应用程序中的默认UIViewController) 。

我该怎么做呢?

我曾经试过改变赛格型,但是根本没有做任何事情。

彼得

你正在寻找的方法是UINavigationController的popToRootViewControllerAnimated:

从文档 :“除了根视图控制器之外,将所有的视图控制器放在堆栈上,并更新显示”。

您需要创build一个自定义后退button。 你不能重写后退button的function。

所以像这样:

 UIButton *myBackButton = [UIButton buttonWithType:UIButtonTypeCustom]; [myBackButton addTarget:self action:@selector(popToRoot:) forControlEvents:UIControlEventTouchUpInside]; UIBarButtonItem *myCustomBackButtonItem = [[UIBarButtonItem alloc] initWithCustomView:myBackButton]; [self.navigationItem setLeftBarButtonItem:myCustomBackButtonItem]; 

然后执行popToRoot:会看起来像:

 - (void)popToRoot:(id)sender { [self.navigationController popToRootViewControllerAnimated:YES]; } 

如果您正在讨论的masterViewController是您可以调用的根:

 [self.navigationController popToRootViewControllerAnimated:YES]; 

您可以自己创build后退button并调用[self.navigationController popToRootViewControllerAnimated:YES] 。 但是,这确实需要您自己创build后退button。 另一个select可能是采取当前的视图控制器,并删除除了第一个和当前的所有这些:

 NSMutableArray *viewControllers = [self.navigationController.viewControllers mutableCopy]; [viewControllers removeObjectsInRange:NSRangeMake(1, [viewControllers count] - 2)]; self.navigationController.viewControllers = viewControllers; 

使用下面的代码为了回到主视图控制器:

 [self.navigationController popViewControllerAnimated:YES]; 

编辑:基于评论,我意识到我的代码只需要你退一步,可能不会回答在这个问题中提出的问题。 谢谢。

我想出了一个在导航控制器集合中查找的解决方法,使用目标控制器的名称find现有和目标控制器之间的偏移量,并删除中间控制器。 不知道它是否接近你的需要,但试试看:

 - (NSArray *)navigateToViewController:(NSString *)destinationControllerName withControllers:(NSArray *)sourceControllers { NSMutableArray *controllers = [[NSMutableArray alloc] initWithArray:sourceControllers]; NSInteger sourceControllerIndex = [controllers count] - 1; // Value should be index oriented instead of position oriented. NSInteger destControllerIndex = 0; // Find position of destination controller (index oriented) for (UIViewController *controller in controllers) { NSString *controllerName = NSStringFromClass([controller class]); NSLog(@"class name: %@", controllerName); if([[controllerName lowercaseString] isEqualToString:[destinationControllerName lowercaseString]]) break; destControllerIndex +=1; } // If desired controller does not exists in the stack, do not proceed. if (destControllerIndex == sourceControllerIndex) { return controllers; } // If destination is the same as source do not enter in this block // If destination and source controllers have zero or no controllers in between do not enter in this block // If destination and source controllers has few controllers (at least one) in between, go inside this block. // Here "destControllerIndex + 1" shows, there is at least one controller in between source and destination. else if(destControllerIndex + 1 < sourceControllerIndex) { NSLog(@"controllers in stack: %@", controllers); // Start from the controller following the source controller in stack and remove it // till the destination controller arrives for (NSInteger iterator = sourceControllerIndex - 1; iterator > destControllerIndex; iterator--) { UIViewController *cont = [controllers objectAtIndex:iterator]; if(cont) { [cont release]; cont = nil; } [controllers removeObjectAtIndex:iterator]; NSLog(@"controllers in stack: %@", controllers); } } return controllers; } 

并在一些基础控制器中定义它:

 -(void) popToViewController:(NSString *)controllerName withNavController:(UINavigationController *)navController { // STStatistics refers a singleton class of common functions. NSArray *mArr = [STStatistics navigateToViewController:controllerName withControllers:navController.viewControllers]; navController.viewControllers = mArr; // There should be more than one controller in stack to pop. if([mArr count] > 1) { [navController popViewControllerAnimated:YES]; } } 

这是怎么叫:

 [self popToViewController:@"NameOfDestinationControllerInNavStack" withControllers:self.navigationController];