gpt4 book ai didi

ios - 访问方法内部的变量并允许使用 segue 将其传递给其他 View Controller

转载 作者:行者123 更新时间:2023-11-28 05:58:47 25 4
gpt4 key购买 nike

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell{
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellIdentifier, for: indexPath) as! FeedCollectionViewCell
cell.postTextView.text = posts.reversed()[indexPath.row].caption
cell.postImageView.image = UIImage(named: "photoplaceholder.jpg")
cell.priceTextView.text = posts.reversed()[indexPath.row].price
cell.categoryTextView.text = posts.reversed()[indexPath.row].category
cell.usernameLabel.text = posts.reversed()[indexPath.row].username
cell.buttonEvents = {
let storyboard = UIStoryboard(name: "Main" , bundle: nil)
let chatViewController =
storyboard.instantiateViewController(withIdentifier: "chat")
self.present(chatViewController, animated: true,completion: nil)
var receiverIDNumber = cell.usernameLabel.text
}

我想将 receiverIDNumber 传递给另一个 View Controller ,但是我做不到,因为变量在方法内部。

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {

let chatViewController = segue.destination as! ChatViewController


chatViewController.receivedString = receiverIDNumber
}

最佳答案

您可以试试这个,因为 prepareForSegue 仅由 performSegue 触发,而不是 present

let chatViewController = storyboard.instantiateViewController(withIdentifier: "chat") as! ChatViewController
chatViewController.receivedString = cell.usernameLabel.text
self.present(chatViewController, animated: true,completion: nil)

关于ios - 访问方法内部的变量并允许使用 segue 将其传递给其他 View Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50554153/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com