我的一个segue从视图控制器转换到tableview控制器。我想在两者之间传递数组,但在tableview控制器之前使用导航控制器,我无法弄清楚如何传递数组。如何通过navigatiom控制器将数据传递给tableview控制器?
答案 0 :(得分:114)
覆盖prepareForSegue
并设置您想要的桌面视图上的任何值。您可以从UINavigation
viewControllers
媒体资源中获取表格视图。
override func prepareForSegue(segue: UIStoryboardSegue!, sender: AnyObject!){
let navVC = segue.destinationViewController as UINavigationController
let tableVC = navVC.viewControllers.first as YourTableViewControllerClass
tableVC.yourTableViewArray = localArrayValue
}
对于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
}
答案 1 :(得分:21)
更新了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
}
}
}
}
答案 2 :(得分:9)
我将此错误发送给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
}
答案 3 :(得分:8)
Tommy的解决方案要求您在故事板中设置Segue。
以下是仅限代码的解决方案:
func functionToPassAsAction() {
var controller: UINavigationController
controller = self.storyboard?.instantiateViewControllerWithIdentifier("NavigationVCIdentifierFromStoryboard") as! UINavigationController
controller.yourTableViewArray = localArrayValue
self.presentViewController(controller, animated: true, completion: nil)
}
答案 4 :(得分:4)
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
}
答案 5 :(得分:2)
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
}
答案 6 :(得分:1)
雨燕5
class MyNavigationController: UINavigationController {
var myData: String!
override func viewDidLoad() {
if let vc = self.topViewController as? MyViewcontoller{
vc.data = self.myData
}
}
}
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)
答案 7 :(得分:0)
在表格视图Swift 4中传递文本 **或仅传递数据但更改功能**
第一视图控制器
班
let name_array = ["BILL GATES", "MARK ZUKERBERG", "DULKAR SALMAN", "TOVINO" , "SMITH"]
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)
第二视图控制器
SecondViewController类:UIViewController {
@IBOutlet weak var lbl2: UILabel!
var str1 = String()
override func viewDidLoad() {
super.viewDidLoad()
lbl2.text = str1
}