我的应用程序的布局是用户需要使用电子邮件和密码(firebase)登录,然后会显示导航栏。当我单击导航栏上的消息选项卡时,没有任何内容出现,它只是空的。当我将消息视图设置为我的初始视图控制器时,消息功能出现并允许我发送消息。
我该如何解决这个问题?我正在使用JSQMessaging
以下是我的" ChatViewController"中的代码。当我通过loginVC注册时,主屏幕会打开一个导航栏。 bar ...当我点击导航栏中的消息时,屏幕显示为全白:
import UIKit
import JSQMessagesViewController
import FirebaseDatabase
import FirebaseAuth
class ChatViewController: JSQMessagesViewController {
var messages = [JSQMessage]()
override func viewDidLoad() {
super.viewDidLoad()
let currentUser = Auth.auth().currentUser
self.senderId = currentUser!.uid
self.senderDisplayName = "Hi"
// Do any additional setup after loading the view.
}
override func didPressSend(_ button: UIButton!, withMessageText text: String!, senderId: String!, senderDisplayName: String!, date: Date!) {
print("didPressSend")
print("\(text)")
messages.append(JSQMessage(senderId: senderId, displayName: senderDisplayName, text: text))
collectionView.reloadData()
print(messages)
}
override func didPressAccessoryButton(_ sender: UIButton!) {
print("AccessoryButton")
let imagePicker = UIImagePickerController()
imagePicker.delegate = self
self.present(imagePicker, animated: true, completion: nil)
}
override func collectionView(_ collectionView: JSQMessagesCollectionView!, messageDataForItemAt indexPath: IndexPath!) -> JSQMessageData! {
return messages[indexPath.item]
}
override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = super.collectionView(collectionView, cellForItemAt: indexPath) as! JSQMessagesCollectionViewCell
return cell
}
override func collectionView(_ collectionView: JSQMessagesCollectionView!, messageBubbleImageDataForItemAt indexPath: IndexPath!) -> JSQMessageBubbleImageDataSource! {
let message = messages[indexPath.item]
if message.senderId == self.senderId{let bubbleFactory = JSQMessagesBubbleImageFactory()
return bubbleFactory?.outgoingMessagesBubbleImage(with: .black)
} else {
let bubbleFactory = JSQMessagesBubbleImageFactory()
return bubbleFactory?.outgoingMessagesBubbleImage(with: .blue)
}
}
override func collectionView(_ collectionView: JSQMessagesCollectionView!, avatarImageDataForItemAt indexPath: IndexPath!) -> JSQMessageAvatarImageDataSource! {
return nil
}
override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
print(messages.count)
return messages.count
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
/*
// MARK: - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
// Get the new view controller using segue.destinationViewController.
// Pass the selected object to the new view controller.
}
*/
}
extension ChatViewController: UIImagePickerControllerDelegate, UINavigationControllerDelegate {
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
print("did finish picking")
print(info)
let picture = info[UIImagePickerControllerOriginalImage] as? UIImage
let photo = JSQPhotoMediaItem(image: picture!)
messages.append(JSQMessage(senderId: senderId, displayName: senderDisplayName, media: photo))
self.dismiss(animated: true, completion: nil)
collectionView.reloadData()
}
}