【问题标题】:Pass messages and/or objects between different storyboards swift在不同的故事板之间快速传递消息和/或对象
【发布时间】:2015-06-29 22:14:38
【问题描述】:

我有两个故事板,需要传递消息和对象。我知道如何在同一个故事板和 .xib 文件中做到这一点,但不知道如何使用两个不同的故事板。

我的代码是:

var storyboard = UIStoryboard(name: "RecibosStoryboard", bundle: nil)
        var controller = storyboard.instantiateViewControllerWithIdentifier("RecibosStoryboard") as! UINavigationController

    self.presentViewController(controller, animated: true, completion: nil).

    // If i do: var controller = storyboard.instantiateViewControllerWithIdentifier("RecibosStoryboard") as! = TableRecibosViewController -> fails ->cannot convert TablaRecibosViewController to UINavigationController

// If i do:

/* var controller = storyboard.instantiateViewControllerWithIdentifier("RecibosStoryboard") as! UINavigationController


 let vartest: TablaRecibosTableViewController = TablaTablaRecibosTableViewController() 

prueba.inicioPrueba(str, strPrueba2: str2) -> two objects are nill

self.presentViewController(controller, animated: true, completion: nil).

 Two objects are nill*/

我的第二个故事板是“RecibosStoryboard”,只有 1 个视图,类是 TablaRecibosViewController 并且有一个构造方法:

class TablaRecibosTableViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {


    @IBOutlet var tablaRecibos: UITableView!
    var pruebaMansajes: String?
    var pruebaMansajes2: String?

    var arrayOfObjectsCellRecibos: [ObjectTableRecibos] = [ObjectTableRecibos] ()

    override func viewDidLoad() {
        super.viewDidLoad()

        tablaRecibos.dataSource = self
        tablaRecibos.delegate = self

        println("Pruebas satisfactorias1 \(pruebaMansajes) \(pruebaMansajes2)")
    }

    func inicioPrueba(strprueba1:String, strPrueba2:String){
    pruebaMansajes = strprueba1
    pruebaMansajes2 = strPrueba2
    }

当我执行应用程序或崩溃或打印两个对象时 = nil

我找不到办法。非常感谢。

【问题讨论】:

    标签: ios swift


    【解决方案1】:

    问题是以下“无法将 TablaRecibosViewController 转换为 UINavigationController”。您的 TablaRecibosViewController 嵌入在 UINavigationController 中。请尝试以下操作:

    if let recibosViewController = (storyboard.instantiateViewControllerWithIdentifier("RecibosStoryboard") as? UINavigationController).viewControllers.first as? TablaRecibosTableViewController {
        recibosViewController.pruebaMensajes = "Hola mundo"
    
    }
    

    您正在创建一个新的 TablaTablaRecibosTableViewController() 实例,它与 Storyboard 没有关联,因此表视图为 nil 并且它崩溃了。

    【讨论】:

    • 我不能用你的代码来做。我没有其他选择将视图放在同一个故事板中:(。出于好奇,如果有人知道怎么做,我认为这对任何人都有帮助。谢谢
    猜你喜欢
    • 2012-01-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-30
    • 2021-09-17
    • 2013-10-06
    • 1970-01-01
    相关资源
    最近更新 更多