Search code examples
iosswiftuiviewcontrolleruisplitviewcontroller

Pass data from UIViewController to UISpiltView


Right now I am facing some problem to pass data from UIViewController to UISpiltViewController. My app start with UIViewController, and after user login it, there is a segue from UIViewController to UISpiltViewController, UISpiltViewController is root view of a NavigationViewController, and this NavigationViewController is a root view of a TableViewController. I have written a class for this TableViewController, called MasterView, and right now I want to pass some information from loginView to this MasterView.

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

But I want to know how to write the prepareforsegue function.

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

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

        controller.authorityNum = self.authorityArray
    }

}

I want to pass an array, but I will get class cast error. Please help me with this.


Solution

  • Try this i am not sure but i think your segue is Model segue so

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

    Try to NSLog every line it may help