将数据从UIViewController传递到UISpiltView

时间:2015-06-30 20:00:53

标签: ios swift uiviewcontroller uisplitviewcontroller

现在我面临一些问题,需要将数据从UIViewController传递到UISpiltViewController。    我的应用程序以UIViewController开头,用户登录后,有一个从UIViewController到UISpiltViewController的segue,UISpiltViewController是NavigationViewController的根视图,这个NavigationViewController是TableViewController的根视图。我已经为这个名为MasterView的TableViewController编写了一个类,现在我想将一些信息从loginView传递给这个MasterView。

  self.performSegueWithIdentifier("login", sender: self)

但我想知道如何编写prepareforsegue函数。

   override func  prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {

    if segue.identifier == "login"
    {
        let controller = (segue.destinationViewController as UINavigationController).topViewController as MasterViewController

        controller.authorityNum = self.authorityArray
    }

}

我想传递一个数组,但是我会遇到类强制转换错误。请帮帮我。

3 个答案:

答案 0 :(得分:1)

尝试这个我不确定,但我认为您的segue 模型赛车所以

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    if segue.identifier == "login"
    {
        var splitView:UISplitViewController = segue.destinationViewController as! UISplitViewController
        var controller = splitView.viewControllers.first?.topViewController as! MasterViewController
        controller.authorityNum = self.authorityArray
    }
}

尝试NSLog可能有用的每一行

答案 1 :(得分:0)

像这样,

let destinationViewController = segue.destinationViewController as! MasterViewController

答案 2 :(得分:0)

Swift 3接受的答案。

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if segue.identifier == "login" {
            let splitView:UISplitViewController = segue.destination as! UISplitViewController
            let navController = splitView.viewControllers.first as! UINavigationController
            let controller = navController?.topViewController as! MasterViewController
            controller.authorityNum = self.authorityArray
    }
}