ios Swift 通过导航控制器传递数据
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25369412/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Swift pass data through navigation controller
提问by user3142972
One of my segues transitions from a view controller to a tableview controller. I want to pass an array between the two, but with a navigation controller before the tableview controller, I can't figure out how to pass the array. How do you pass data through a navigatiom controller to a tableview controller?
我的其中一个转场从视图控制器转换到表视图控制器。我想在两者之间传递一个数组,但是在 tableview 控制器之前使用导航控制器,我无法弄清楚如何传递数组。您如何通过导航控制器将数据传递到 tableview 控制器?
回答by Tommy Devoy
Override prepareForSegue
and set whatever value on the tableview you'd like. You can grab the tableview from the UINavigation
viewControllers
property.
覆盖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
}
For Swift 3 :
对于 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
}
回答by Indrajit Sinh Rayjada
@Tommy Devoy's answer is correct but here is the same thing in swift 3
@Tommy Devoy 的回答是正确的,但在 swift 3 中也是一样的
Updated 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
}
}
}
}
回答by mythicalcoder
I was getting this errorto Horatio's solution.
我在Horatio 的解决方案中遇到了这个错误。
ERROR: Value of type 'UINavigationController' has no member 'yourTableViewArray'
ERROR: Value of type 'UINavigationController' has no member 'yourTableViewArray'
So this might help others like me looking for a code
only solution. I wanted to redirect to a Navigation controller, yet pass data to root view controller within that Nav Controller.
所以这可能会帮助像我这样的人寻找code
唯一的解决方案。我想重定向到导航控制器,但将数据传递到该导航控制器内的根视图控制器。
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
}
回答by LamaTo
SWIFT 3 (tested solution) - Passing data between VC - Segue with NavigationController
SWIFT 3(经过测试的解决方案)-在 VC-Segue 与 NavigationController 之间传递数据
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
let navigationContoller = segue.destination as! UINavigationController
let receiverViewController = navigationContoller?.topViewController as ReceiverViewController
receiverViewController.reveivedObject = sentObject
}
回答by Horatio
Tommy's solution requires you to set up the Segue in Storyboard.
Tommy 的解决方案要求您在 Storyboard 中设置 Segue。
Here is a code only solution:
这是一个仅代码解决方案:
func functionToPassAsAction() {
var controller: UINavigationController
controller = self.storyboard?.instantiateViewControllerWithIdentifier("NavigationVCIdentifierFromStoryboard") as! UINavigationController
controller.yourTableViewArray = localArrayValue
self.presentViewController(controller, animated: true, completion: nil)
}
回答by MANISH PATHAK
FIXED syntax for SWIFT 3
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
}
回答by Faizal Nowshad KN
passing a text in tableview swift 4**or just pass data but change the function **
在 tableview swift 4 中传递文本** 或者只是传递数据但更改函数 **
First View Controller
第一个视图控制器
class
班级
let name_array = ["BILL GATES", "MARK ZUKERBERG", "DULKAR SALMAN", "TOVINO" , "SMITH"]
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath){
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath){
let story: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let new = story.instantiateViewController(withIdentifier: "SecondViewController")as! SecondViewController
new.str1 = name_array[indexPath.row]
self.navigationController?.pushViewController(new, animated: true)
Second View Controller
第二个视图控制器
class SecondViewController: UIViewController {
类 SecondViewController: UIViewController {
@IBOutlet weak var lbl2: UILabel!
var str1 = String()
override func viewDidLoad() {
super.viewDidLoad()
lbl2.text = str1
}