如何将数据从一个视图控制器传输到另一个视图

时间:2018-04-06 18:57:50

标签: ios swift

我试图在第二个视图控制器中获取一个文本字段,以显示在第一个视图控制器中单击的单元格的名称。我已经尝试了所有可以找到的教程,但是使用当前代码,它在第二个视图控制器中将值打印为nil,即使它在第一个视图控制器中打印单元格作为单元格的值。这是我目前的代码

NearbyViewController

     func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    let cell = UITableViewCell(style: .default, reuseIdentifier: "cell")
    var (cellName) = myList[indexPath.row]

     let viewController = storyboard?.instantiateViewController(withIdentifier: "Home2") as! ShopViewController
    viewController.name = cellName
    navigationController?.pushViewController(viewController, animated: true)

    let vct = self.storyboard?.instantiateViewController(withIdentifier: "Home2")
    self.present(vct!, animated: true, completion: nil)


    print("row\(indexPath.row)")
    print("name: \(cellName)")


}

ShopViewController

var name :String?

@IBOutlet weak var myTextField: UITextField!



override func viewDidLoad() {

        print(name)



}

5 个答案:

答案 0 :(得分:0)

您似乎正在创建2个视图控制器。 instantiateViewController(withIdentifier: "Home2") 您首先更改值然后按,然后再次显示它。所以你看到的控制器是第二个,你没有设置值。

删除

let vct = self.storyboard?.instantiateViewController(withIdentifier: "Home2") self.present(vct!, animated: true, completion: nil)

答案 1 :(得分:0)

删除以下行:

let vct = self.storyboard?.instantiateViewController(withIdentifier: "Home2")
self.present(vct!, animated: true, completion: nil)  

您已经在navigationController中推送了相同的viewController 你的最终代码应该是:

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    let cell = UITableViewCell(style: .default, reuseIdentifier: "cell")
    var (cellName) = myList[indexPath.row]

     let viewController = storyboard?.instantiateViewController(withIdentifier: "Home2") as! ShopViewController
    viewController.name = cellName
    navigationController?.pushViewController(viewController, animated: true)
    print("row\(indexPath.row)")
    print("name: \(cellName)")
}  

只有在storyboard中设置了viewController的标识符后,才能生效。这就是你如何做到的:

enter image description here

答案 2 :(得分:0)

我使用以下代码解决了我的问题:

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    let cell = UITableViewCell(style: .default, reuseIdentifier: "cell")
    var (cellName) = myList[indexPath.row]

     let viewController = self.storyboard?.instantiateViewController(withIdentifier: "Home2") as! ShopViewController
    viewController.name = cellName
            self.present(viewController, animated: true, completion: nil)
    navigationController?.pushViewController(viewController, animated: true)


    print("row\(indexPath.row)")
    print("name: \(cellName)")


}

答案 3 :(得分:0)

尝试使用Objective-c:

    UIStoryboard *story = [UIStoryboard storyboardWithName:@"Main" bundle:nil];

    SecondViewController *nextView= [story instantiateViewControllerWithIdentifier:@"SecondViewController"];  // Please set "**SecondViewController**" to the secondController's storyboardID from storyboard. 

    nextView.strUserName = @"username" ;   // or you can transfer any type of data array or dictionary.

    [self.navigationController pushViewController:nextView animated:YES];

答案 4 :(得分:-1)

viewDidLoad变量设置之前调用函数name

请尝试将print()移至viewWillAppear