passing data with performSegueWithIdentifier nil

醉酒当歌 提交于 2019-12-06 16:24:40

问题


I have the segue setup as:

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?


回答1:


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
        }
    }
}

For Swift 3

override func prepare(for segue: UIStoryboardSegue, sender: Any?) 
    if segue.identifier == "showIndividualChat" {
        if let friendChat = sender as? Friend {
            let controller = segue.destination as! IndividualChatController
            controller.friendChat = friendChat
            controller.senderId = Global.sharedInstance.userID
            controller.senderDisplayName = Global.sharedInstance.userName
        }
    }
}


来源:https://stackoverflow.com/questions/37581531/passing-data-with-performseguewithidentifier-nil

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!