使用 UID 作为属性创建 segue



我想创建一个segue,通过帖子创建者的给定userID引导当前用户聊天vc:

我现在的续集:

var user: User?
let chatLogController = ChatLogController(collectionViewLayout: UICollectionViewFlowLayout())
chatLogController.user = user
DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) {
self.navigationController?.pushViewController(chatLogController, animated: true)
}

ChatLogController.user:

var user: User?   {
didSet {
navigationItem.title = user?.name
observeMessages()
}
}

和用户类:

class User: NSObject {
var id: String?
var name: String?
var email: String?
var profileImageUrl: String?
init(dictionary: [String: AnyObject]) {
self.id = dictionary["id"] as? String
self.name = dictionary["name"] as? String
self.email = dictionary["email"] as? String
self.profileImageUrl = dictionary["profileImageUrl"] as? String
}

}

我现在想要的是,segue直接转到特定用户的chatLogController。 我知道我需要连接我要连接的用户的 uid,我也在

var jobCreatorID: String?

但是我需要在多大程度上更改我的代码才能使其正常工作?

你应该改变这个

var user: User?

由于您在此将此用户设置为下一个视图控制器

let chatLogController = ChatLogController(collectionViewLayout: UICollectionViewFlowLayout())
chatLogController.user = user

但是这个用户是 Avobe 声明为用户的用户吗? 所以不是用户实例。 如果此类有一个用户对象,那么你应该断言该用户

chatLogController.user = self.user

并在您的 ChatLogController 中viewDidLoad设置标题并获取用户 ID,如下所示:

override func viewDidLoad() {
super.viewDidLoad()
navigationItem.title = self.user
}

因此,您从该用户那里获得了ID。

最新更新