使用performSegueWithIdentifier nil传递数据

时间:2016-06-02 02:07:16

标签: ios swift segue

我将segue设置为:

enter image description here

和tableView行选择:

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")

    }
}

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

但是,目标控制器中friendChat中的对象controller.friendChat始终为零。

如何传递数据:

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

成功到目标控制器?

1 个答案:

答案 0 :(得分:5)

您在didSelectRowAtIndexPath中所做的第一件事是取消选择该行,因此当您尝试访问prepareForSegue中的所选行时,您将不会选择任何行。

由于您将Friend实例作为sender传递给performSegueWithIdentifier,您可以在let friendChat = sender as? Friend中说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
        }
    }
}

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