我希望当用户选择一行时,通过segue更改另一个视图的标签。
我成功检索了选定的行,但无法将值归因于postText?
这是文件SelectionMarque swift:
class SelectionMarqueViewController: UITableViewController {
var posts = [Post]()
var indexMarques = 0
var marqueSelected: String = ""
override func viewDidLoad() {
super.viewDidLoad()
tableView.dataSource = self
loadMarques()
}
func loadMarques() {
var ref : DatabaseReference?
ref = Database.database(url: "https://myURL.com/").reference()
//ref?.observe(.childAdded, with: { (snapshot) in
ref!.queryOrdered(byChild: "Marque").observe(.childAdded, with: { (snapshot) -> Void in
if let dict = snapshot.value as? [String: Any] {
let marqueText = dict["Marque"] as! String
let post = Post(marqueText: marqueText)
self.posts.append(post)
print(marqueText)
self.tableView.reloadData()
}
})
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return posts.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "PostCell", for: indexPath)
cell.textLabel?.text = self.posts[indexPath.row].marque
return cell
}
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
marqueSelected = posts[indexPath.row].marque
print("Marque selectionnée \(marqueSelected)")
self.performSegue(withIdentifier: "goToChoiceGeneral", sender: nil)
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
var marqueToSent = segue.destination as! ChoiceGeneralTableViewController
marqueToSent.postText = marqueSelected
print("Marque sent \(marqueSelected)")
}
}
这里是另一个视图:
class ChoiceGeneralTableViewController: UITableViewController {
@IBOutlet weak var marquePressed: UILabel!
@IBOutlet weak var labelMarque: UILabel!
public var postText: String? = ""
override func viewDidLoad() {
super.viewDidLoad()
labelMarque.text = postText
}
}
我尝试了很多事情,但实际上控制台返回了:
Marque sent
Marque selectionnée Honda
Marque sent Honda
我认为问题可能来自Marque sent
第一次出现nil,第二次出现该值。我认为segue会执行nil值而不是Honda,这就是为什么我的标签为nil。
感谢您的帮助