我有一个名为'detailViewController'的UIViewController。
使用push segue通过多个不同的segue访问此视图控制器。
我面临的问题是detailViewController上的后退按钮将用户带回到前一个视图控制器(应该如此),但是我希望后退按钮将用户带到masterViewController(默认的UIViewController)应用程序)。
我该怎么做?
我已经尝试过更改segue类型但是根本没有做任何事情。
彼得
答案 0 :(得分:3)
您正在寻找的方法是UINavigationController的popToRootViewControllerAnimated:
从documentation:“弹出除根视图控制器之外的堆栈上的所有视图控制器并更新显示。”
您需要创建自定义后退按钮。你不能忽略后退按钮的功能。
类似于:
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];
}
答案 1 :(得分:0)
使用以下代码返回主视图控制器:
[self.navigationController popViewControllerAnimated:YES];
修改强> 基于这些评论,我意识到我的代码只带你退一步,可能无法回答这个问题中的问题。感谢。
答案 2 :(得分:0)
如果您所谈论的masterViewController
是您可以致电的根:
[self.navigationController popToRootViewControllerAnimated:YES];
答案 3 :(得分:0)
您可以自己创建后退按钮并拨打[self.navigationController popToRootViewControllerAnimated:YES]
。但是,这确实需要您自己创建后退按钮。另一种选择可以是获取当前视图控制器,并删除除第一个和当前视图控制器之外的所有视图控制器:
NSMutableArray *viewControllers = [self.navigationController.viewControllers mutableCopy];
[viewControllers removeObjectsInRange:NSRangeMake(1, [viewControllers count] - 2)];
self.navigationController.viewControllers = viewControllers;
答案 4 :(得分:0)
我想出了一个在导航控制器集合中查找的解决方法,并使用目标控制器的名称查找现有控制器和目标控制器之间的偏移量,并删除中间控制器。不确定它是否接近你的需要,但试一试:
- (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];