I am new to Swift. I am trying to pass data between two View Controllers. The value I pass through from the first view controller is nil when used by the second view controller. I do not understand why this is happening.
This is my tableView
function call in the first view controller. It gets the selected table cell's user information.
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
let user = User()
if(messages.count > 0) {
let message = self.messages[indexPath.row]
guard let chatPartnerID = message.chatPartnerID() else {
return
}
let ref = FIRDatabase.database().reference().child("users").child(chatPartnerID)
ref.observeSingleEventOfType(.Value, withBlock: { (snapshot) in
guard let dictionary = snapshot.value as? [String: AnyObject]
else {
return
}
user.id = chatPartnerID
user.setValuesForKeysWithDictionary(dictionary)
self.setupChatLogControllerForUser(user)
}, withCancelBlock: nil)
}else {
let user = users[indexPath.row]
self.setupChatLogControllerForUser(user)
}
}
User
in an NSObject
which I declare like so:
class User: NSObject {
var id: String?
var name: String?
var email: String?
var profileImageUrl: String?
}
The setupChatLogControllerForUser(user)
method in the first view controller takes in the user parameter that is given in the tableView
method and passed onto the second view controller (ChatLogController
) like this:
func setupChatLogControllerForUser(user: User) {
let chatLogController = ChatLogController()
chatLogController.selectedUserID = user.id!
chatLogController.user = user
}
My second View Controller method receives the data with this:
var selectedUserID = String()
var user = User()
The second view controller also has a handleSend
method which is linked to a button on my storyboard through IBAction
.
func handleSend() {
let ref = FIRDatabase.database().reference().child("messages")
let childRef = ref.childByAutoId()
let toID = self.selectedUserID
let fromID = FIRAuth.auth()!.currentUser!.uid
let timestamp: NSNumber = Int(NSDate().timeIntervalSince1970)
let values = ["text": messageInputField.text!, "toID": toID, "fromID": fromID, "timestamp": timestamp]
childRef.updateChildValues(values) { (error, ref) in
if error != nil {
print(error)
return
}
let userMessagesRef = FIRDatabase.database().reference().child("userMessages").child(fromID)
let messageID = childRef.key
userMessagesRef.updateChildValues([messageID: 1])
let recipientUserMessagesRef = FIRDatabase.database().reference().child("userMessages").child(toID)
recipientUserMessagesRef.updateChildValues([messageID: 1])
}
}
This method is where the problem is occurring. I set toID
equal to the selectedUserID
. In the debugger, I can see the selectedUserID
populated with a string when the view controller segues to the ChatLogController
. Why is the value of selectedUserID
variable nil when the method is called?
I understand that passing in the selectedUserID
and the User
object is redundant, I just wanted to see if the problem was something with the String variable being passed between the classes, but it stays nil.
Any help would be awesome!
EDIT: I am using a segue to switch between my view controllers