通过导航控制器快速传递数据


68

我的选择之一是从视图控制器过渡到表视图控制器。我想在两者之间传递一个数组,但是在tableview控制器之前使用导航控制器,所以我不知道如何传递该数组。如何通过导航控制器将数据传递到表视图控制器?

Answers:


131

重写prepareForSegue并在所需的表视图上设置任何值。您可以从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   
}

对于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   
}

21
代替navVC.viewControllers.first,可以执行navVC.topViewController
Arnaud

如果它是从topViewController选择的视图控制器怎么办?
Jenita _Alice4Real

24

@汤米·德沃伊(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 


            }

        }

    }

}

13

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 
    }

1
非常好。TY TY
Justin

12

我在Horatio的解决方案中遇到此错误

ERROR: Value of type 'UINavigationController' has no member 'yourTableViewArray'

因此,这可能会帮助像我这样的其他人寻找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
}

1
Perfecto解决方案,只是为了澄清一下,如果从AppDelegate呈现,请使用self.window?....,并保留当前的注释,如果从ViewController呈现,则注释// self.window?...,并取消注释present(controller
Alejandro Luengo

1
是的,因为present()需要ViewController。是不是 但是自从我联系上已经有一段时间了。您可以通过实际测试来确认,并可能在评论中留下评论或建议编辑。我一定会更新。
mythicalcoder

1
是的,它的确定,self.window .rootViewController =控制器如果从AppDelegate中和现在提出的(控制器,动画:真,完成:无)如果从视图控制器呈现,只需选择你的情况和评论另一个?
亚历杭德罗·Luengo

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)
}

2
错误:类型'UINavigationController'的值没有成员'yourTableViewArray'!(使用Swift3)
mythicalcoder

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
    }

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)

0

在表格视图中快速传递文本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)

第二视图控制器

class SecondViewController:UIViewController {

@IBOutlet weak var lbl2: UILabel!

var str1 = String()

override func viewDidLoad() {
    super.viewDidLoad()

     lbl2.text =  str1

}
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.