【问题标题】:Using Ray's MVC demo, and want to implement TableViewDelegate使用Ray的MVC demo,想实现TableViewDelegate
【发布时间】:2017-09-26 04:01:12
【问题描述】:

我正在尝试使用https://github.com/raywenderlich/mvc-modern-approach 中的代码 遵循这种模式来解耦事情非常好。 但我被困在某一点上。 我想在调用 cell 的 didSelect 时推送另一个带有参加者(cell 模型)详细信息的控制器。

我们如何使用通用 ui 来实现这一点?

【问题讨论】:

    标签: ios swift model-view-controller mvvm


    【解决方案1】:

    已解决: 我的问题是基于 GitHub 中的 MVC 演示,并使用了该设计模式。

    在 TableViewDataSource 中,我添加了一个变量

    var actionHandler: ((Model) -> Void)?
    

    并在 didSelect 中调用它。

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        actionHandler?(dataSource[indexPath.row])
    }
    

    在 UIController 中

    collectionViewDataSource.actionHandler = { [weak self] (model) in
        guard let strongSelf = self else { return }
        if let cellTapped = strongSelf.viewModel?.didSelectItem {
            cellTapped(model)
        }
    }
    

    【讨论】:

      【解决方案2】:

      1- 在情节提要中选择您要呈现的视图控制器,并在类名下放置任何情节提要 ID 示例:“MyViewController”。

      2- 然后在 didSelectRowAtIndexPath 中写入以下内容:

       let vc = self.storyboard?.instantiateViewController(withIdentifier: "MyViewController") as! MyViewController
        //here you can pass any variable example: vc.variable = myVariable
      
        DispatchQueue.main.async(execute: {
              self.navigationController?.pushViewController(vc, animated: true)
      
          });
      

      【讨论】:

        【解决方案3】:

        当我正确理解你时,你会尽量避免使用 segue 的 InterfaceBuilder。 您是否尝试使用 UIViewController 中的 present 功能?

        func present(_ viewControllerToPresent: UIViewController, animated flag: Bool, completion: (() -> Swift.Void)? = nil)
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2023-04-02
          • 1970-01-01
          相关资源
          最近更新 更多