【问题标题】:Switch between different Views in SWIFT在 SWIFT 中的不同视图之间切换
【发布时间】:2015-10-24 03:56:52
【问题描述】:

我在堆栈溢出社区中阅读了很多关于在不同 ViewController 之间切换的问题/答案,但我找不到针对我的具体问题的正确答案。

  1. 我正在使用 NavigationViewController 来“连接”我所有不同的 viewController(ViewController / TableViewController / CollectionViewController)

  2. 我有一些不依赖于 ViewController 类的类。例如在我的“计算”类中计算任何东西。

我正在寻找一种简单的解决方案,以便在我的所有 ViewController 之间切换。我想在我的“计算”课上做决定,例如在我的导航视图控制器中后退一步。

在我使用这个的那一刻:

  1. 在两个不同的 ViewController 之间切换的示例:

    let sb = UIStoryboard(name: "Main", bundle: nil)
    let vc = sb.instantiateViewControllerWithIdentifier("StartPage") as! StartPageView
    self.presentViewController(vc, animated: true, completion: nil)
    

问题:此示例仅适用于 ViewController 类,导航栏消失。

  1. 在我的 NavigationViewController 中退一步的示例:

    self.navigationController?.popViewControllerAnimated(true)
    

问题:此示例仅适用于 ViewController 类。

编辑 1:

我找到了一个很好的解决方案,可以用这部分代码调用我的所有视图控制器:

    let switchViewController = self.navigationController?.viewControllers[1] as! ScanTableView
    self.navigationController?.popToViewController(switchViewController, animated: true)

如何在非 ViewController 类中调用此代码?

【问题讨论】:

    标签: ios swift uitableview uiviewcontroller uinavigationcontroller


    【解决方案1】:

    将新的视图控制器推送到您现有的导航堆栈上,而不是模态地呈现它:

    let vc = self.storyboard!.instantiateViewControllerWithIdentifier("StartPage") as! StartPageView
    self.navigationController!.pushViewController(vc, animated: true)
    

    【讨论】:

    • 这段代码可以很好地从一个 ViewController 切换到另一个!现在导航栏不会消失。但我无法在 ViewController 类之外的另一个类中使用此代码。我收到错误消息:“TEST_CLASS 类型的值没有成员 'storyboard'” 是否有可能修复此错误?谢谢!
    • 您的核心还有另一个问题。当我调用它时,两个 ViewController 之间的切换运行良好,但创建了一个新的后退按钮。有可能防止这种情况吗?我想恢复旧的。
    猜你喜欢
    • 1970-01-01
    • 2011-09-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-21
    • 1970-01-01
    相关资源
    最近更新 更多