简体   繁体   English

将数据从UIViewController传递到UISpiltView

[英]Pass data from UIViewController to UISpiltView

Right now I am facing some problem to pass data from UIViewController to UISpiltViewController. 现在,我面临将数据从UIViewController传递到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. 我的应用程序从UIViewController开始,并且在用户登录后,从UIViewController到UISpiltViewController有一个序列,UISpiltViewController是NavigationViewController的根视图,而该NavigationViewController是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. 我为此TableViewController编写了一个类,称为MasterView,现在我想将一些信息从loginView传递到此MasterView。

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

But I want to know how to write the prepareforsegue function. 但是我想知道如何编写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
    }

}

I want to pass an array, but I will get class cast error. 我想传递一个数组,但是我会得到类强制转换错误。 Please help me with this. 请帮我解决一下这个。

Try this i am not sure but i think your segue is Model segue so 试试这个我不确定,但我认为您的segue是Model 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
    }
}

Try to NSLog every line it may help 尝试对每条线进行NSLog可能会有所帮助

像这样,

let destinationViewController = segue.destinationViewController as! MasterViewController

Swift 3 of the accepted answer. 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
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将数据从UIViewController传递到AppDelegate - Pass data from UIViewController to AppDelegate 将数据从 AppDelegate 传递到 UIViewController - Pass Data From AppDelegate into UIViewController 如何在Swift中将数据从UITableViewRowAction传递到UIViewController? - How to pass data to a UIViewController from a UITableViewRowAction in Swift? 从 UIViewController 传递 NSArray - Pass NSArray from UIViewController 如何通过UITapGestureRecognizer将数据从一个UIViewController传递到另一个UIViewController - How to pass data from one UIViewController to another one through UITapGestureRecognizer 如何将数据从基于plist的UITableView传递到UIViewController - How to pass data from plist based UITableView to UIViewController 如何将数字或数据从子类 UIView 传递到 UIViewController? - How to pass digit or data from subclassed UIView to UIViewController? 我如何将数据从UIViewController传递到SWRevealViewController - How can i pass data from UIViewController to SWRevealViewController Swift-如何将数据从UITableViewCell类传递到UIViewController - Swift - How to Pass Data From UITableViewCell Class to UIViewController 从UIViewController传递到另一个类时丢失数据 - Losing data when pass from UIViewController to another class
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM