当前位置: 首页 > 知识库问答 >
问题:

如何替换视图控制器(或从导航堆栈中删除),而不是推送序列?

巫马化
2023-03-14

我有一个小型iPhone应用程序,它使用导航控制器显示3个视图(此处为全屏):

首先,它显示社交网络列表(Facebook、Google等):

然后它会显示一个OAuth对话框,要求提供凭据:

以及(之后,在相同的UIWebView中)权限:

最后,它显示带有用户详细信息的最后一个视图控制器(在真正的应用程序中,这将是菜单,可以在其中开始多人游戏):

这一切都很好,但我有一个问题,当用户想要返回并选择另一个社交网络时:

用户触摸后退按钮,而不是显示第一个视图,而是显示第二个视图,再次请求OAuth凭据/权限。

我能在这里做什么?Xcode 5.0.2显示的segue选择非常有限——推送、模态(我不能使用,因为它模糊了我游戏所需的导航栏)和自定义。

我是一个iOS编程新手,但之前我开发了一个Adobe AIR移动应用程序,可以1)替换视图而不是推送,2)从导航堆栈中删除不需要的视图。

如何在本机应用程序中执行相同操作?

共有3个答案

万俟浩
2023-03-14

自定义segue不适合我,因为我有一个Splash view控制器,我想更换它。由于列表中只有一个视图控制器,因此popToRootViewController仍然在堆栈上留下了亮点。我使用以下代码来替换单个控制器

-(void)perform {
    UIViewController *sourceViewController = (UIViewController*)[self sourceViewController];
    UIViewController *destinationController = (UIViewController*)[self destinationViewController];
    UINavigationController *navigationController = sourceViewController.navigationController;
    [navigationController setViewControllers:@[destinationController] animated:YES];
}

现在在Swift 4中:

class ReplaceSegue: UIStoryboardSegue {

    override func perform() {
        source.navigationController?.setViewControllers([destination], animated: true)
    }
}

现在在Swift 2.0中

class ReplaceSegue: UIStoryboardSegue {

    override func perform() {
        sourceViewController.navigationController?.setViewControllers([destinationViewController], animated: true)
    }
}
萧业
2023-03-14

您可以使用自定义segue:要做到这一点,您需要创建一个子类UIStoryboardSegue(例如MyCustomSegue),然后您可以使用如下内容覆盖“perform”

-(void)perform {
    UIViewController *sourceViewController = (UIViewController*)[self sourceViewController];
    UIViewController *destinationController = (UIViewController*)[self destinationViewController];
    UINavigationController *navigationController = sourceViewController.navigationController;
    // Pop to root view controller (not animated) before pushing
    [navigationController popToRootViewControllerAnimated:NO];
    [navigationController pushViewController:destinationController animated:YES];    
}

此时转到Interface Builder,选择“自定义”segue,并将您的类的名称(例如MyCustomSegue)

在雨燕

override func perform() {
    if let navigationController = self.source.navigationController {
        navigationController.setViewControllers([self.destination], animated: true)
    }
}
扶珂
2023-03-14

为了扩展上面的各种扩展,这是我的解决方案。它有以下优点:

  • 可以在视图堆栈中的任何位置工作,而不仅仅是顶视图(不确定这是否真的需要,甚至在技术上是否可以触发,但它就在那里)。
  • 在显示替换之前,它不会导致弹出或过渡到以前的视图控制器,它只是显示具有自然过渡的新控制器,背面导航与源控制器的背面导航相同。

序列代码:

- (void)perform {
    // Grab Variables for readability
    UIViewController *sourceViewController = (UIViewController*)[self sourceViewController];
    UIViewController *destinationController = (UIViewController*)[self destinationViewController];
    UINavigationController *navigationController = sourceViewController.navigationController;

    // Get a changeable copy of the stack
    NSMutableArray *controllerStack = [NSMutableArray arrayWithArray:navigationController.viewControllers];
    // Replace the source controller with the destination controller, wherever the source may be
    [controllerStack replaceObjectAtIndex:[controllerStack indexOfObject:sourceViewController] withObject:destinationController];

    // Assign the updated stack with animation
    [navigationController setViewControllers:controllerStack animated:YES];
}
 类似资料:
  • 问题内容: 在我的视图控制器代码中,我该如何区分: 模态呈现 推入导航堆栈 这两个和是在这两种情况下,所以都不是很有益的。 使事情变得复杂的是,我的父视图控制器有时是模态的,将要检查的视图控制器推到该模态上。 事实证明,我的问题是,我嵌入我的,然后呈现。这就是为什么我自己的尝试和下面的好答案无法正常工作的原因。 我想我最好告诉我的视图控制器何时是模态的,而不是试图确定。 问题答案: 拿一粒盐,没有

  • 在我的视图控制器代码中,我如何区分: 以模式呈现 推式导航堆栈 在这两种情况下,

  • 问题内容: 题 如何仅使用按钮的touch up内部事件从一个视图控制器导航到另一个视图控制器? 更多信息 我在一个示例项目中尝试执行的步骤是: 创建示例单视图应用程序。 为用户界面(ViewController2)添加一个新文件->具有XIB的Objective-C类。 在ViewController.xib中添加一个按钮,并控制单击ViewController.h的按钮以创建内部补全事件。 转

  • 我需要一些关于react原生应用程序的社区建议。我是一个全新的人,不理解其中的一些基本区别。 关于反应本地文档创建StackNavigator的示例建议如下:从“反应导航堆栈”导入{createStackNav导航仪}; 我发现很多人使用下面的样式导入{堆栈导航器,Tab导航器}从“反应导航”; 为什么人们使用以上两种不同的风格?哪一个更合适,为什么? react-navigation-stack

  • 我有一个导航视图控制器,它会在操作时向选项卡栏视图控制器发送一个序列。因此,选项卡式视图控制器继承了导航栏。我正在尝试将标题应用于连接到选项卡栏视图控制器的其中一个视图控制器,但通过代码设置标题对我不起作用。有人知道为什么会这样吗? 这是我的故事板的图片: 带有注销按钮的视图控制器是我试图在导航条(代码)中设置标题的地方: 导航控制器中嵌入的视图控制器触发到选项卡栏控制器的顺序:

  • 我已经有一个stack类在工作,但是现在我每次只能弹出一个元素,我希望能够同时弹出多个元素。无需多次使用推送和弹出。我试图创建两个函数来实现这一点。函数pushAll和popN。比如: 备注:输入参数all是一个数组,包含所有必须输入的元素。向量的最后一个元素必须在叠加后位于顶部。如果没有空间插入所有元素,则必须不插入任何元素,并且必须引发StackFullException异常。popN方法弹出