swift - 将对象从 tableview 传递到目的地,其间有 RevealViewController

标签 swift uitableview swrevealviewcontroller

我试图将一个对象从我的表格 View 传递到详细 View 。我正在使用 RevealViewController 框架来提供滑出菜单。因此,我需要创建一个从 tableview 到 RevealViewController 的 Segue,并从这里创建另一个到最终的DetailViewController 的 Segue。

这就是为什么我无法在详细 View 中设置对象 - 知道如何做到这一点吗?

这是使用的代码:

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

  }
}

这是一个异常(exception)。

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

关于swift - 将对象从 tableview 传递到目的地,其间有 RevealViewController,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36607329/

相关文章:

ios - 如何编辑 UIScrollView 大小?

ios - 如何在具有单独位置 url 和资源 url 的 swift 中从服务器直播音频?

iphone - UITableView 不滚动到单元格

ios - 在运行时重新排 ListView 中的单元格

ios - 使用 swrevealviewcontroller 从 uitableviewcell 导航到多个 uinavigationcontroller

arrays - 快速查找字符串数组之间的匹配项

ios - 如何使用 SpriteKit 将节点固定在某个位置

iphone - 如何在 iPhone 上创建 TableView 层次结构?

ios - swift : Disable FrontViewController when Rear is Displayed

ios - 带有 SWRevealViewController 的 Exc_bad_instruction