我的一个segue从视图控制器转换到tableview控制器.我想在两者之间传递数组,但在tableview控制器之前使用导航控制器,我无法弄清楚如何传递数组.如何通过navigatiom控制器将数据传递给tableview控制器?
Tom*_*voy 121
覆盖prepareForSegue
并设置您想要的tableview上的任何值.您可以从UINavigation
viewControllers
酒店获取tableview .
override func prepareForSegue(segue: UIStoryboardSegue!, sender: AnyObject!){
let navVC = segue.destinationViewController as UINavigationController
let tableVC = navVC.viewControllers.first as YourTableViewControllerClass
tableVC.yourTableViewArray = localArrayValue
}
Run Code Online (Sandbox Code Playgroud)
对于Swift 3:
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
let navVC = segue.destination as? UINavigationController
let tableVC = navVC?.viewControllers.first as! YourTableViewControllerClass
tableVC.yourTableViewArray = localArrayValue
}
Run Code Online (Sandbox Code Playgroud)
Ind*_*ada 22
@Tommy Devoy的答案是正确的,但在swift 3中这是同样的事情
更新了Swift 3
// MARK: - Navigation
//In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
// Get the new view controller using segue.destinationViewController.
// Pass the selected object to the new view controller.
if segue.identifier == "yourSegueIdentifier" {
if let navController = segue.destination as? UINavigationController {
if let chidVC = navController.topViewController as? YourViewController {
//TODO: access here chid VC like childVC.yourTableViewArray = localArrayValue
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
myt*_*der 10
我把这个错误归咎于Horatio的解决方案.
ERROR: Value of type 'UINavigationController' has no member 'yourTableViewArray'
所以这可能会帮助像我这样的人寻找code
唯一的解决方案.我想重定向到导航控制器,然后将数据传递到该Nav Controller中的根视图控制器.
if let controller = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "yourNavigationControllerID") as? UINavigationController,
let yourViewController = controller.viewControllers.first as? YourViewController {
yourViewController.yourVariableName = "value"
self.window?.rootViewController = controller // if presented from AppDelegate
// present(controller, animated: true, completion: nil) // if presented from ViewController
}
Run Code Online (Sandbox Code Playgroud)
Tommy的解决方案要求您在Storyboard中设置Segue.
这是一个仅限代码的解决方案:
func functionToPassAsAction() {
var controller: UINavigationController
controller = self.storyboard?.instantiateViewControllerWithIdentifier("NavigationVCIdentifierFromStoryboard") as! UINavigationController
controller.yourTableViewArray = localArrayValue
self.presentViewController(controller, animated: true, completion: nil)
}
Run Code Online (Sandbox Code Playgroud)
SWIFT 3(经测试的解决方案)-在VC之间传递数据-使用NavigationController进行Segue
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
let navigationContoller = segue.destination as! UINavigationController
let receiverViewController = navigationContoller?.topViewController as ReceiverViewController
receiverViewController.reveivedObject = sentObject
}
Run Code Online (Sandbox Code Playgroud)
雨燕5
class MyNavigationController: UINavigationController {
var myData: String!
override func viewDidLoad() {
if let vc = self.topViewController as? MyViewcontoller{
vc.data = self.myData
}
}
}
Run Code Online (Sandbox Code Playgroud)
let navigation = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "MyNavigationController") as! MyNavigationController
navigation.myData = "Data that you want to pass"
present(navigation, animated: true, completion: nil)
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
37287 次 |
最近记录: |