swift - 将数据从 View Controller 传递到 tabbarcontroller

标签 swift xcode

我从 View Controller 开始,我想通过按下按钮转到选项卡栏 Controller 。

 @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)
    }

完美衔接。但是,我尝试通过执行此操作将数据传递到选项卡栏的第一个 View Controller

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

    }
}

我的问题是,在目标 View Controller 中,我包含了一条打印语句来查看数据是否已传递,但应用程序在打印语句上崩溃。

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

最佳答案

这一行

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

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

//

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

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

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

if let indexPath = sender as? IndexPath 

关于swift - 将数据从 View Controller 传递到 tabbarcontroller,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51029896/

相关文章:

ios - 键盘未加载或速度太慢

Swift:INImage 不遵守指定的宽度和高度

objective-c - Xcode 不会打开我的项目

swift - Xcode 7.3.1 Swift "initializer does not override a designated initializer from its superclass"错误

swift - 无法将 UIViewController 类型的值转换为 RecordDrawerController

objective-c - 如何使用 Swift 中的 AXUIElementCopyAttributeValue?

ios - 使用 Xcode 出现错误 "No such module",但框架在那里

从 Storyboard 或 Code 进行 IOS 设计

ios - 更改首先打开的 View (Xcode)

ios - Discard all changes in Xcode source control 实际上做了什么?