Sauron Sauron - 6 months ago 58
Swift Question

passing data with performSegueWithIdentifier nil

I have the segue setup as:

enter image description here

and the tableView row selection:

override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {

tableView.deselectRowAtIndexPath(indexPath, animated: true)

// Ensure controller knows which dataset to pull from,
// so detail view is correct
var friendChat: Friend!
if searchController.active && searchController.searchBar.text != "" {
friendChat = filterMappedFriends[indexPath.row]
} else {
friendChat = mappedFriends[indexPath.row]
}

// Now set the conditional cases: if a friend then chat, if user then friend request if not user then can invite them:
if(friendChat.statusSort == 2) {

self.performSegueWithIdentifier("showIndividualChat", sender: friendChat)

} else if (friendChat.statusSort == 1) {

print("Can invite to be friend")

} else if (friendChat.statusSort == 0) {

print("Invite to Feast")

}
}


and the prepareForSegue:

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {

if let indexPath = tableView.indexPathForSelectedRow {

// Ensure controller knows which dataset to pull from,
// so detail view is correct
let friendChat: Friend
if searchController.active && searchController.searchBar.text != "" {
friendChat = filterMappedFriends[indexPath.row]
} else {
friendChat = mappedFriends[indexPath.row]
}

// Now set the conditional cases: if a friend then chat, if user then friend request if not user then can invite them:

if segue.identifier == "showIndividualChat" {

let controller = segue.destinationViewController as! IndividualChatController
controller.friendChat = friendChat
controller.senderId = Global.sharedInstance.userID
controller.senderDisplayName = Global.sharedInstance.userName
}
}
}


However, the object
friendChat
, seen in
controller.friendChat
, of the destination controller is always nil.

How can I pass the data:

controller.friendChat = friendChat
controller.senderId = Global.sharedInstance.userID
controller.senderDisplayName = Global.sharedInstance.userName


to the destination controller successfully?

Answer

The first thing you are doing in didSelectRowAtIndexPath is deselecting the row, so when you try and access the selected row in prepareForSegue you are going to get no row selected.

Since you are passing the Friend instance as your sender to performSegueWithIdentifier you can just say let friendChat = sender as? Friend in prepareForSegue;

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) 
    if segue.identifier == "showIndividualChat" {
        if let friendChat = sender as? Friend {
            let controller = segue.destinationViewController as! IndividualChatController
            controller.friendChat = friendChat
            controller.senderId = Global.sharedInstance.userID
            controller.senderDisplayName = Global.sharedInstance.userName
        }
    }
}
Comments