在导航控制器中的视图控制器之间传递数据

时间:2018-07-22 13:59:35

标签: swift xcode uiviewcontroller uinavigationcontroller

所以我有一个导航控制器和两个视图控制器。第一个视图控制器显示一个带有一些文本的文本标签,一个用于输入食品配料列表的文本字段和一个按钮,该按钮应将我们带入第二个视图控制器,该控件将从文本输入中列出一个列表领域。以下代码适用于第一个视图控制器:

import UIKit

class ViewController: UIViewController {
@IBOutlet weak var foodImage: UIImageView!
@IBOutlet weak var searchField: UITextField!

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if (segue.identifier == "goSegue"){
        if (searchField.text != ""){
            let listOfFood = segue.destination as! ListOfFoodViewController
            listOfFood.commaString = searchField.text!
        }
        else if (searchField.text == ""){
            let alertEmpty = UIAlertController(title: "Emptyness", message: "There is no input!", preferredStyle: .alert)
            let defaultAction = UIAlertAction(title: "Understood", style: .default, handler: nil)
            alertEmpty.addAction(defaultAction)
            self.present(alertEmpty, animated: true, completion: nil)
        }
    }
}

在情节提要中,我使用按钮对第二个视图控制器(称为ListOfFoodViewController)进行了segue(我称为“ goSegue”)。我第二个视图控制器的代码是

import UIKit

class ListOfFoodViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
var commaString = ""
var listOfTags = [String]()
let simpleTableIdentifier = "SimpleTableIdentifier"

//DATA SOURCE METHODS
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return listOfTags.count
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    var cell = tableView.dequeueReusableCell(withIdentifier: simpleTableIdentifier)
    if (cell == nil) {
        cell = UITableViewCell(style: UITableViewCellStyle.default, reuseIdentifier: simpleTableIdentifier)
    }
    cell?.textLabel?.text=listOfTags[indexPath.row]
    return cell!
}

override func viewDidLoad() {
    super.viewDidLoad()
    self.commaString = ""
    self.listOfTags = self.commaString.components(separatedBy: ",")
    // Do any additional setup after loading the view.
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

因此,用户必须输入用逗号分隔的单词列表,然后我用它制作一个listOfTags并将其放在表中。因此,在segue之前,应调用函数prepare(for segue ...),在这里我将创建第二个视图控制器的实例(具有commaString属性),并将输入分配给commaString,如下所示:

let listOfFood = segue.destination as! ListOfFoodViewController
listOfFood.commaString = searchField.text!

但是当我运行该程序时,除了tableview为空之外,其他所有东西都起作用!我不知道为什么这行不通。这与我正在使用导航控制器有关吗?

2 个答案:

答案 0 :(得分:1)

这是因为您在viewDidLoad

中清除了它
 self.commaString = ""

因此注释该行并像这样声明commaString

var commaString = "" {
  didSet {
      self.listOfTags = self.commaString.components(separatedBy: ",")
  }

}

//

self.tableView.delegate = self
self.tableView.dataSource = self

答案 1 :(得分:0)

在ViewController中,您说的是:

listOfFood.commaString = searchField.text!

在ListOfFoodViewController中,您说的是:

self.commaString = ""

因此,无论将ViewController放入ListOfFoodViewController中,ListOfFoodViewController都会立即将其删除,最后我们得到一个空表。