将对象从 tableview 传递到目的地,中间有 revealviewcontroller
Pass object from tableview to destination with revealviewcontroller in between
我正在尝试将一个对象从我的表格视图传递到详细信息视图。我正在使用 revealviewcontroller 框架来制作滑出式菜单。因此,我需要创建一个从 tableview 到 revealviewcontroller 的 segue,然后从这里创建另一个到最终的 detailviewcontroller。
这就是我无法在详细视图中设置对象的原因 - 知道如何操作吗?
这是使用的代码:
if segue.identifier == "communityDetailSegue" {
// Get the cell that generated this segue.
if let selectedCommunityCell = sender as ? UITableViewCell {
let destination = segue.destinationViewController as!CommunityViewController
if let communityIndex = self.tableView.indexPathForCell(selectedCommunityCell) {
destination.community = self.communitiesOfCurrentUser[communityIndex.row]
print(self.communitiesOfCurrentUser[communityIndex.row].name)
}
}
}
而这次是例外。
Could not cast value of type 'SWRevealViewController' (0x10027b9f0) to 'CommunityViewController'
您收到错误是因为 segue 的目的地 VC 是 SWRevealViewController
而不是 CommunityViewController
。
解决您的问题的一种方法是分两步传递值:
首先,在 prepareForSegue()
中,您将值传递给 SWRevealViewController(您需要为此创建一个子类,例如 MyRevealViewController
):
if segue.identifier == "communityDetailSegue" {
// Get the cell that generated this segue.
if let selectedCommunityCell = sender as ? UITableViewCell {
let destination = segue.destinationViewController as! MyRevealViewController
if let communityIndex = self.tableView.indexPathForCell(selectedCommunityCell) {
destination.community = self.communitiesOfCurrentUser[communityIndex.row]
print(self.communitiesOfCurrentUser[communityIndex.row].name)
}
}
}
然后,在MyRevealViewController
中,您可以在设置后立即传递值:
class MyRevealViewController : SWRevealViewController {
// Let's assume this is the outlet to your final VC:
IBOutlet let communityViewController: CommunityViewController!
var community: YourCommunityType {
didSet {
if let communityVC = self.communityViewController {
communityVC.community = self.community
}
}
}
}
我正在尝试将一个对象从我的表格视图传递到详细信息视图。我正在使用 revealviewcontroller 框架来制作滑出式菜单。因此,我需要创建一个从 tableview 到 revealviewcontroller 的 segue,然后从这里创建另一个到最终的 detailviewcontroller。
这就是我无法在详细视图中设置对象的原因 - 知道如何操作吗?
这是使用的代码:
if segue.identifier == "communityDetailSegue" {
// Get the cell that generated this segue.
if let selectedCommunityCell = sender as ? UITableViewCell {
let destination = segue.destinationViewController as!CommunityViewController
if let communityIndex = self.tableView.indexPathForCell(selectedCommunityCell) {
destination.community = self.communitiesOfCurrentUser[communityIndex.row]
print(self.communitiesOfCurrentUser[communityIndex.row].name)
}
}
}
而这次是例外。
Could not cast value of type 'SWRevealViewController' (0x10027b9f0) to 'CommunityViewController'
您收到错误是因为 segue 的目的地 VC 是 SWRevealViewController
而不是 CommunityViewController
。
解决您的问题的一种方法是分两步传递值:
首先,在 prepareForSegue()
中,您将值传递给 SWRevealViewController(您需要为此创建一个子类,例如 MyRevealViewController
):
if segue.identifier == "communityDetailSegue" {
// Get the cell that generated this segue.
if let selectedCommunityCell = sender as ? UITableViewCell {
let destination = segue.destinationViewController as! MyRevealViewController
if let communityIndex = self.tableView.indexPathForCell(selectedCommunityCell) {
destination.community = self.communitiesOfCurrentUser[communityIndex.row]
print(self.communitiesOfCurrentUser[communityIndex.row].name)
}
}
}
然后,在MyRevealViewController
中,您可以在设置后立即传递值:
class MyRevealViewController : SWRevealViewController {
// Let's assume this is the outlet to your final VC:
IBOutlet let communityViewController: CommunityViewController!
var community: YourCommunityType {
didSet {
if let communityVC = self.communityViewController {
communityVC.community = self.community
}
}
}
}