【问题标题】:Prepare for Segue to UITabBarController Tab准备 Segue 到 UITabBarController 选项卡
【发布时间】:2015-04-11 00:44:22
【问题描述】:

我想将信息从ViewController 传递到UITabBarController,以便我可以从UITabBarController 的第三个选项卡中的视图控制器访问某些信息。

但是,问题是当我尝试这样做时,我的程序不断崩溃。到目前为止,这是我的代码:

我这样称呼segue:

self.firstName = user.first_name
self.lastName = user.last_name
self.performSegueWithIdentifier("OffersView", sender: self)

我重写了prepareForSegue 函数,这样我就可以像这样在这个函数中传递信息:

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    if(segue.identifier == "OffersView"){
        let barViewControllers = segue.destinationViewController as UITabBarController
        let destinationViewController = barViewControllers.viewControllers![2] as ProfileController
        destinationViewController.firstName = self.firstName
        destinationViewController.lastName = self.lastName
    }
}

当我尝试设置destinationViewController(在上面代码的第二行)时,我的代码崩溃了。我不知道为什么,因为我查看了许多 StackOverflow 帖子,例如

Swift tab bar view prepareforsegue 和 Pass data from tableview to tab bar view controller in Swift. 但都没有太大的成功。我是否可能需要创建一个UITabBarControllerDelegate 类并通过它传递信息?任何提示将不胜感激。谢谢!

【问题讨论】:

  • 它崩溃并显示什么错误消息?
  • @rdelmar 它似乎没有显示任何特定的错误消息。
  • 在测试应用程序中,将两个“as”单词更改为“as!”后,您的代码对我来说运行良好在prepareForSegue中。
  • 你能举个例子说明你是怎么做到的吗?我在“as”字前添加了一个感叹号,但我收到一条错误消息,提示“As 之后的预期类型”@rdelmar
  • 它在 as, as! 之后,而不是之前。

标签: ios iphone swift segue


【解决方案1】:

经过讨论发现,问题在于标签栏控制器的子级嵌入在导航控制器中,因此需要将代码更改为这样,

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) { 
    let barViewControllers = segue.destinationViewController as! UITabBarController 
    let nav = barViewControllers.viewControllers![2] as! UINavigationController 
    let destinationViewController = nav.topviewcontroller as ProfileController 
    destinationViewController.firstName = self.firstName
    destinationViewController.lastName = self.lastName 
}

【讨论】:

    【解决方案2】:

    在 swift 3 中,xcode 8 中的代码会是这样的

    let barViewControllers = segue.destination as! UITabBarController
    let nav = barViewControllers.viewControllers![0] as! UINavigationController
    let destinationViewController = nav.viewControllers[0] as! YourViewController
            destinationViewController.varTest = _varValue
    

    【讨论】:

    • 如何找出选择了哪个选项卡并为该特定选项卡视图设置变量?
    • 就我而言,我需要导航到“YourViewController”。我的猜测是在 viewControllers 数组中存储了所有控制器(我是 swift 新手)。
    【解决方案3】:

    Swift 4 没有强制可选解包的解决方案

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        if let barVC = segue.destination as? UITabBarController {
            barVC.viewControllers?.forEach {
                if let vc = $0 as? YourViewController {
                    vc.firstName = self.firstName
                    vc.lastName = self.lastName
                }
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2012-05-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-08-15
      • 2014-08-12
      相关资源
      最近更新 更多