【问题标题】:Swift 3+: Multiple Storyboards with TabBarController. Set TabBarController Tab, Segue to desired ViewControllerSwift 3+:带有 TabBarController 的多个故事板。设置 TabBarController 选项卡,Segue 到所需的 ViewController
【发布时间】:2018-12-15 07:31:37
【问题描述】:

在多个故事板中工作。从点 A->TabBarController(tab 2)->NavigationControllerA->StoryboardReference->NavigationControllerB->ViewControllerA->ViewControllerB 获取。无需数据传递。

我的标签栏控制器设置在自己的故事板中,每个标签都通过导航控制器到达故事板引用。然后情节提要参考链接到另一个情节提要(obv.),并通过导航控制器链接到 ViewControllerA。然后我想去performSegue到ViewControllerB。这一切都是通过 UIViewController 扩展完成的。 根据我一直在阅读的内容,我应该这样处理:

    let tab = 2
    let sbOne = UIStoryboard(name: "Main", bundle: nil)
    let tabBarController = sbOne.instantiateViewController(withIdentifier: "TabBarController") as! UITabBarController
    let appDelegate = UIApplication.shared.delegate as! AppDelegate
    appDelegate.window?.rootViewController = tabBarController
    tabBarController.selectedIndex = tab

    let sbTwo = UIStoryboard(name: "sbTwo", bundle: nil)
    let viewControllerA = sbTwo.instantiateViewController(withIdentifier: "ViewControllerA")
    performSegue(withIdentifier: "toViewControllerB", sender: self)

我收到 NSInvalidArgument:A 点“没有带有标识符 'toViewControllerB' 的 segue”

【问题讨论】:

    标签: swift delegates storyboard viewcontroller tabbarcontroller


    【解决方案1】:

    斯威夫特 3+ 如果这是最好的答案,我会感到惊讶,但这是唯一有效的解决方案。我的一些问题源于缺乏理解。但我得出的概念是将功能分成两部分。设置 tabbarcontroller 选项卡,然后使用变量结构转到目的地。 我最终在 A 点设置了一个结构。

    struct Destination {
        static var currentID = Int()
    }
    

    按下按钮时,我会用这个设置变量,然后调用下面的函数:

    Destination.currentID = currentID
    goToViewControllerA()
    

    然后使用我之前的部分代码,我可以将标签栏控制器设置为根视图控制器并转到所选标签上的 ViewControllerA(第一个视图控制器)。这是在 uiviewcontroller 扩展中完成的,但也可以在 Point A 中的函数中完成。

    goToViewControllerA(){
    let tab = 2
    let sbOne = UIStoryboard(name: "Main", bundle: nil)
    let tabBarController = sbOne.instantiateViewController(withIdentifier: "TabBarController") as! UITabBarController
    let appDelegate = UIApplication.shared.delegate as! AppDelegate
    appDelegate.window?.rootViewController = tabBarController
    tabBarController.selectedIndex = tab
    }
    

    然后只需检查是否设置了 struct static var,然后转到目标控制器 (ViewControllerB)。这是在 ViewControllerA 中完成的,或者如果您必须通过多个视图控制器进行连接,您可以在 viewdidload 中使用它在每个视图控制器上为您的函数建模。

    If Destination.currentID != Int() {
        performSegue(withIdentifier:“toViewControllerBSegue”, sender:self)
    }
    

    如果存在的话,我很想学习一种更好的方法。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-10-22
      • 2015-02-25
      • 2018-02-16
      • 2016-10-23
      • 2014-01-23
      • 2016-03-03
      • 1970-01-01
      相关资源
      最近更新 更多