将数据从视图控制器传递到TabbarController

时间:2018-06-25 18:35:10

标签: swift xcode

我从一个视图控制器开始,我想按一下按钮选择一个标签栏控制器。

 @IBAction func goToReplyProfile(_ sender: Any) {
        let storyBoard = UIStoryboard(name: "Main", bundle: nil)
        let vc = storyBoard.instantiateViewController(withIdentifier: "CommentTabBarController") as! TTabBarViewController

        self.present (vc, animated: false, completion: nil)
    }

最完美的对话。但是,我尝试通过执行此操作

将数据传递到选项卡栏的第一个视图控制器
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if let vc = (segue.destination as? UITabBarController)?.viewControllers?.first as? CommentProfileViewController {

        let testing = "practice"
        vc.Author = testing

    }
}

我的问题是,在目标视图控制器中,我包含了一条print语句,以查看是否传递了数据,但应用程序在该print语句上崩溃了。

print(Author! + "should print out testing")

1 个答案:

答案 0 :(得分:0)

此行

 self.present (vc, animated: false, completion: nil)

与prepareForSegue无关,它实际上不在其中运行您的代码,因此,导致崩溃的nil作者,您真正需要的是使用performSegue(withIdentifier调用prepare

//

问题在于您是作为发件人发送自己的

 performSegue(withIdentifier: "FirstVC", sender: self)

在这里,您将其转换为IndexPath,这肯定会失败

if let indexPath = sender as? IndexPath 
相关问题