firebase ios聊天应用程序无法运行

时间:2017-05-31 17:49:46

标签: ios swift firebase firebase-realtime-database

我有一个tableViewController,它有一个登录用户与他/她选择的任何用户有过的消息列表。单击单元格时,它应该转到视图控制器,允许登录用户与任何用户聊天。我已经设置了一个覆盖segue:

var userpicuid: String?
var username: String?

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

         let message = messages[indexPath.row]

        if message.ReceiverId != self.loggedInUserUid {
            var newVariable = message.ReceiverId

            if self.userpicuid ==  newVariable {

                let ref = FIRDatabase.database().reference().child("users").child(userpicuid!)

                ref.observeSingleEvent(of: .value, with: { (snapshot)
                    in

                    if let dictionary = snapshot.value as? [String: AnyObject]{
                        for post in dictionary {
                            let messages = post.value as! [String: AnyObject]
                            for (id, value) in messages {

                                self.username = messages["username"] as? String


                            }}}})}} else if message.senderId != self.loggedInUserUid {
            let newVariable = message.senderId

            if self.userpicuid ==  newVariable {

                let ref = FIRDatabase.database().reference().child("users").child(userpicuid!)

                ref.observeSingleEvent(of: .value, with: { (snapshot)
                    in

                    if let dictionary = snapshot.value as? [String: AnyObject]{
                        for post in dictionary {
                            let messages = post.value as! [String: AnyObject]
                            for (id, value) in messages {

                                self.username = messages["username"] as? String
                            }}}})}

        }

    performSegue(withIdentifier: "MessageNow", sender: self.userpicuid)

}


override public func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    guard segue.identifier == "MessageNow", let chatVc = segue.destination as? SendMessageViewController else {
        return
    }

    chatVc.senderId = self.loggedInUser?.uid
    chatVc.receiverData = sender as AnyObject
    chatVc.senderDisplayName = self.userpicuid
    chatVc.username = self.username

}

在目标视图控制器中,这是设置:

var receiverData: AnyObject?

override func viewDidLoad() {
        super.viewDidLoad()

        self.senderId = FIRAuth.auth()?.currentUser?.uid

 let receiverId = receiverData as! String
        let receiverIdFive = String(receiverId.characters.prefix(5))
        let senderIdFive = String(senderId.characters.prefix(5))
        if (senderIdFive > receiverIdFive)
        {
            self.convoId = senderIdFive + receiverIdFive
        }
        else
        {
            self.convoId = receiverIdFive + senderIdFive
        }}

我收到错误无法将类型为“Lit_Swap.MessageTableViewCell”(0x1091d0f10)的值转换为“NSString”(0x10b153c60)。因为发件人显然是一个tableviewcell,我将如何设置发件人,这样我就可以点击单元格并进入聊天控制器。

2 个答案:

答案 0 :(得分:0)

有很多方法可以在视图之间移动数据,一种是segue,而且还有很多segue选项。

这里使用的是RepresentObject属性

假设我们有一个带有textField的视图控制器和一个触发secondSheetController动作的按钮

class ViewController: NSViewController {

   @IBOutlet weak var sourceTextField: UITextField!

   override func viewDidLoad() {
       super.viewDidLoad()
   }

   override func prepare(for segue: NSStoryboardSegue, sender: Any?) {
       let second = segue.destinationController as! SecondController
       second.representedObject = self.myTextField.text
   }
}

而viewController我们是

class SecondController: NSViewController {

   @IBOutlet weak var targetTextField: NSTextField!

   override func viewDidLoad() {
       super.viewDidLoad()
   }

   override func viewWillAppear() {
       self.targetTextField.text = self.representedObject as! String
   }
}

另外,为了解决发件人是String的问题而且我们不想使用representObject属性,对String的赋值将是一个解决方案。

chatVc.receiverData = sender as! String

作为第三个选项,可以直接设置SecondController中的任何类变量

class SecondController: NSViewController {
   var option1 = ""
   var option2 = ""

然后从第一个视图控制器

secondController.option1 = "look kids, Parliament, Big Ben"
secondController.option2 = "Don't call me Shirley"

答案 1 :(得分:0)

如果您使用的是UINavigatorcontroll。你可以尝试这个解决方案。

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

    let selectedConversation = conversationArray[indexPath.row]
    showConversationMessages(conversation: selectedConversation)
}


func showConversationMessages(conversation: ConversationItem){
    let vc = storyboard?.instantiateViewController(withIdentifier: "MessagesViewController") as! MessagesVC
    vc.receiverId = conversation.idMessagePartner
    vc.receiverToken = conversation.receiverToken
    vc.receiverUserName = conversation.userDisplayName
    navigationController?.pushViewController(vc, animated: true)
}

ConversationItem是我的班级,用于保存我需要的数据。

然后你可以在你的第二个控制器中使用变量,我在我的例子中命名为MessageVC

class MessagesVC: UIViewController, UITableViewDelegate, UITableViewDataSource, UITextViewDelegate {
    private let TAG = "MessagesVC"

    var receiverId: Int64 = 0
    var receiverToken: String = ""
    var receiverUserName: String = ""

  ....

我从未真正与override func prepare(for segue: NSStoryboardSegue, sender: Any?)合作,但我确实看到很多人都遇到过问题。