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

标签 ios swift firebase

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/

相关文章:

iOS UIInputViewController : present Custom UIViewController + NIB

swift - 从 swift 1.0 到 1.2 的固定功能

ios - 如何从 UiTableViewCell 更改 View Controller 中字典的值

Firebase 远程配置替代方案

ios - 在 xCode/swift 中将元素的边缘与 super View 的中心对齐

iphone - 应用程序在启动期间崩溃并出现 valueForUndefinedKey 错误

firebase - 已删除的 Firebase 用户仍然可以进行身份​​验证

firebase - Firebase 云消息传递 (FCM) 诊断告诉我们什么?

javascript - 无法在 Android 设备上运行 React-Native 示例代码

ios - 如何设置 uiview 背景和 uiimageview 居中、缩放以填充和 mask 到边界?