3

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

使用 push segue 通过多个不同的 segue 访问此视图控制器。

我面临的问题是 detailViewController 上的后退按钮将用户带回到前一个视图控制器(应该如此),但是我希望后退按钮将用户带到 masterViewController(应用程序中的默认 UIViewController) .

我该怎么做呢?

我曾尝试更改 segue 类型,但实际上并没有做任何事情。

彼得

4

5 回答 5

3

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

文档中:“弹出堆栈上的所有视图控制器,除了根视图控制器并更新显示。”

您需要创建一个自定义后退按钮。您不能 afaik 覆盖后退按钮的功能。

所以像:

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];
}
于 2014-02-27T17:41:41.087 回答
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];
于 2014-02-27T18:07:57.777 回答
0

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

[self.navigationController popToRootViewControllerAnimated:YES];
于 2014-02-27T17:42:24.430 回答
0

使用以下代码返回主视图控制器:

[self.navigationController popViewControllerAnimated:YES];

编辑: 根据评论,我意识到我的代码只会让你退后一步,可能无法回答这个问题中的问题。谢谢。

于 2014-02-27T17:39:52.583 回答
0

您可以自己创建后退按钮并调用[self.navigationController popToRootViewControllerAnimated:YES]. 但是,这确实需要您自己创建后退按钮。另一种选择是获取当前视图控制器,并删除除第一个和当前视图控制器之外的所有视图控制器:

NSMutableArray *viewControllers = [self.navigationController.viewControllers mutableCopy];
[viewControllers removeObjectsInRange:NSRangeMake(1, [viewControllers count] - 2)];
self.navigationController.viewControllers = viewControllers;
于 2014-02-27T17:43:16.043 回答