【发布时间】:2023-03-12 17:34:01
【问题描述】:
我有一个聊天应用程序,想通过使用模态转场从我的ChatController 转到newMessageVC。在newMessageVC 中,我的所有联系人都显示在表格视图中。当点击一个联系人时,newMessaenter code heregeVC 应该关闭并且点击的联系人应该被发送到ChatController。此时,ChatController 应该执行 segue 以转到 ChatVC 以显示与单击用户的新聊天。我该怎么做?
我尝试了以下代码:
newMessageVC 正在关闭,函数showChatVC 被调用(它显示了打印语句),但随后出现了错误,尽管 segue 确实存在。从ChatController 到ChatVC 有一个名为showChatVC 的segue
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)
}
}
}
我做错了什么?
【问题讨论】:
标签: swift modal-dialog segue dismiss