2017-05-31 88 views
0

我有了消息登錄的用戶的列表已經與他/她選擇的任何用戶有一個tableViewController。單擊單元格時,它應該轉到一個視圖控制器,允許登錄用戶與任何用戶聊天。我已經建立了一個覆蓋賽格瑞:火力IOS聊天應用程序無法正常工作

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,我將如何設置發件人,這樣我就可以點擊該單元格並進入聊天控制器。

+0

這將有助於具體瞭解哪條線路導致錯誤。設置一個斷點並手動逐行逐行顯示包含錯誤的行。但是,我懷疑這行* chatVc.receiverData =發件人爲AnyObject *,它將發件人分配爲一個值,與此行不匹配* let receiverId = receiverData as! String * – Jay

+0

@Jay線路導致錯誤是'let receiverId = receiverData as!字符串' – juelizabeth

+0

沒錯。根據我的評論。您不能將* sender *分配給字符串,因爲它是不同的類。 *(覆蓋公共FUNC準備(對於SEGUE:UIStoryboardSegue,發件人:任意){(* – Jay

回答

0

有很多的方法來視圖之間移動數據,一個是賽格瑞並有許多用於segue'ing以及選項。

下面是一個使用representedObject財產

假設我們有一個文本框,並觸發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 
    } 
} 

而且我們segue'ing到

的的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屬性,分配給字符串將是一個解決方案。

chatVc.receiverData = sender as! String 

作爲第三個選擇,任何類在SecondController瓦爾可能是如果使用一個UINavigatorcontroll直接設置

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

然後從第一視圖控制器

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

可以請你,如果你可以向我解釋爲什麼這樣做@IBAction func sendMessage(_ sender:Any){在其他視圖控制器中執行在我的問題完全相同的重寫公共職能工程。 – juelizabeth

+0

不確定這是你問的,但*發件人:任何?* vs *發件人:self.userpicuid *是一個重大的區別。 *任何*是任何對象(目前未定義),而* uidpicid *是一個字符串。 – Jay

+0

所以基本上我想傳遞字符串self.userpicuid所以我改變chatvc.receiverdata到'chatVc.receiverData = self.userpicuid',並在chatviewcintroller我設置'var receiverData:String?'和'讓receiverId = receiverData!'但現在我收到錯誤:意外地發現零,同時展開一個可選值 – juelizabeth

0

。你可以試試這個解決方案。

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?)合作過,但我確實看到很多有問題的人。