访问方法中的变量并允许使用 segue 将其传递给其他视图控制器


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
}

我想将接收器IDNumber传递给另一个视图控制器,但是我无法这样做,因为变量在方法内部。

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)

您需要在呈现之前添加chatViewController.receivedString,如下所示:

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

相关内容

  • 没有找到相关文章

最新更新