[英]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.