【问题标题】:Passing Data Forward In Swift Without Segue在没有 Segue 的情况下在 Swift 中转发数据
【发布时间】:2019-01-23 07:56:32
【问题描述】:

I'm currently making a to do list app using Swift 4. The home view controller has a tableview with some categories in it and when one is selected, it takes the user to a view controller where the items in that category are列出。但是我有一个错误,因为列表中只显示了最近的项目。

我认为这是由于我导航到列表视图控制器的方式。我目前正在这样做:

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    let destinationVC = ListVC()
    if let indexPath = tableView.indexPathForSelectedRow {
        destinationVC.selectedCategory = categoryArray[indexPath.row]
    }
    navigationController?.pushViewController(destinationVC, animated: true)
    
    tableView.deselectRow(at: indexPath, animated: true)
}

在列表视图控制器中,我只需要这个来加载数据:

var selectedCategory : Category? {
    didSet {
        loadItems()
    }
}

我首先使用故事板创建了这个应用程序,当使用 segues 时,它工作得非常好。

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    performSegue(withIdentifier: "goToItems", sender: self)
}

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    let destinationVC = segue.destination as! TodoListVC
    
    if let indexPath = tableView.indexPathForSelectedRow {
        destinationVC.selectedCategory = categoryArray[indexPath.row]
    }
}

所以基本上,问题是在二级列表视图控制器中,即使它们存储在核心数据中,它也只会显示最近添加的项目而不会显示其他项目。我认为这与我每次创建新对象时显示辅助视图控制器的方式有关。

如何正确转到下一个视图控制器?

【问题讨论】:

  • 漏洞在loadItems

标签: ios swift core-data segue


【解决方案1】:

删除segue并添加storyboard id

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        let vc = self.storyboard?.instantiateViewController(withIdentifier: "storyboard_id") as! TodoListVC
        vc.selectedCategory = categoryArray[indexPath.row]
        self.navigationController?.pushViewController(vc, animated: true)
    }

【讨论】:

    【解决方案2】:

    您可以使用故事板实例将数据从一个视图控制器发送到另一个视图控制器。

    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { 
    
       let next = self.storyboard?.instantiateViewController(withIdentifier: "NextControllerStoryBoard_id")as! NextController
    
    next.selectedCategory = categoryArray[indexPath.row]
    
    self.navigationController?.pushViewController(next, animated: true) }
    

    这里 NextController 是你想要去的控制器类名。selectedCategory 是你在 NextController 上声明的数组名

    您可以发送任何数组字典、图像、Int 值等。

    NextControllerStoryBoard_id 是您在该控制器的情节提要中声明的 id

    在故事板 ID 中添加您的故事板 ID:

    【讨论】:

      【解决方案3】:

      我认为通过这段代码我已经感觉到您正在以不正确的方式将数据传递给另一个视图控制器:

      let destinationVC = ListVC()
      if let indexPath = tableView.indexPathForSelectedRow {
          destinationVC.selectedCategory = categoryArray[indexPath.row]
      }
      

      ...

      我的建议是,不要以这种方式传递数据,您必须使用数组传递一个包含所选类别中的项目的数组,然后通过准备转场传递该数组。

      然后从接收视图控制器中的 viewdidappear 或 viewdidload 方法,使用从源 VC 传递的数组,并将其用作第二个 VC 中表视图的数据源。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-01-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多