我有一个聊天应用程序,想通过使用模态搜索从我的ChatController转到newMessageVC。在newMessageVC中,我的所有联系人都显示在表格视图中。单击联系人时,应关闭newMessageVC并将单击的联系人发送到ChatController。此时,ChatController应该执行搜索以转到ChatVC以显示与单击的用户的新聊天。我该怎么办?
我尝试了以下代码:
关闭了newMessageVC,并调用了showChatVC函数(它显示了print语句),但是尽管确实存在segue,但还是出现了Error。从ChatController到ChatVC,有一个名为“ showChatVC”的段
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Receiver (<WeAreFriends.ChatController: 0x7fd760e22e80>) has no segue with identifier 'showChatVC''
class ChatController : UITableViewController {
//MARK: - Outlets
@IBOutlet weak var MessageTableView: UITableView!
//MARK: - Properties
var messages = [MessageModel]()
//Mark: - Init View Lifecycle
override func viewDidLoad() {
super.viewDidLoad()
configureNavigationBar()
}
//MARK: - Navigation Bar
func configureNavigationBar() {
navigationItem.title = "Messages"
navigationController?.navigationBar.barTintColor = Defaults.hexStringToUIColor(hex: "#006D79")
let textAttributes = [NSAttributedString.Key.foregroundColor: Defaults.hexStringToUIColor(hex: "#FFAA01")]
navigationController?.navigationBar.titleTextAttributes = textAttributes
navigationItem.rightBarButtonItem = UIBarButtonItem(barButtonSystemItem: .add, target: self, action: #selector(handleNewMessage))
}
@objc func handleNewMessage(){
performSegue(withIdentifier: "showNewMessage", sender: self)
}
//MARK: - Show ChatVC
func showChatVC(forUser user: UserModel){
print("Shooow, jihaaaaa")
let messageController = MessageController()
messageController.user = user
performSegue(withIdentifier: "showChatVC", sender: self)
}
//MARK: - Prepare for segue
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "showNewMessage" {
let destinationVC = segue.destination as! NewMessageController
destinationVC.chatController = ChatController()
}
}
//MARK: - Table Config
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return messages.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "MessagesTableViewCell", for: indexPath) as! MessagesTableViewCell
return cell
}
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
print("did select row")
}
}
newMessageVC:
class NewMessageController: UITableViewController {
//MARK: - Outlets
@IBOutlet var NewMessageTableView: UITableView!
//MARK: - Var/let
var users = [UserModel]()
var chatController : ChatController?
//Mark: - Init View Lifecycle
override func viewDidLoad() {
super.viewDidLoad()
configureNavigationBar()
fetchUser()
}
//MARK: - User laden
func fetchUser(){
UserApi.shared.observeUsersButCurrentUser { (user) in
self.users.append(user)
self.NewMessageTableView.reloadData()
}
}
//MARK: - Navigation Bar
func configureNavigationBar() {
navigationItem.title = "New Message"
navigationController?.navigationBar.barTintColor = Defaults.hexStringToUIColor(hex: "#006D79")
let textAttributes = [NSAttributedString.Key.foregroundColor: Defaults.hexStringToUIColor(hex: "#FFAA01")]
navigationController?.navigationBar.titleTextAttributes = textAttributes
}
//MARK: - Tableview Config
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return users.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "NewMessageTableViewCell", for: indexPath) as! NewMessageTableViewCell
cell.user = users[indexPath.row]
return cell
}
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
self.dismiss(animated: true) {
let user = self.users[indexPath.row]
self.chatController!.showChatController(forUser: user)
}
}
}
我在做什么错了?
答案 0 :(得分:0)
替换
destinationVC.chatController = ChatController()
使用
destinationVC.chatController = self
ChatController()
是一个新的实例,没有附加情节提要布局,因此没有停工,因此崩溃了
func showChatVC(forUser user: UserModel){
performSegue(withIdentifier: "showChatVC", sender:user)
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "showNewMessage" {
let destinationVC = segue.destination as! NewMessageController
destinationVC.chatController = ChatController()
}
else if segue.identifier == "showChatVC" {
let destinationVC = segue.destination as! ChatVC
destinationVC.user = sender as! UserModel
}
}
也可以
weak var chatController : ChatController?
为了不引起保留周期