【问题标题】:iOS Development: How to dynamically fill a 'label' field in swift from a model?iOS 开发:如何从模型中快速动态填充“标签”字段?
【发布时间】:2022-07-29 17:24:35
【问题描述】:

这里是my code on GitHub that has screen shots of what I am trying to accomplish and what I currently have

我关注this YouTube video 想出了我现在拥有的东西。

Using data from this api,我通过从 model.swift 文件中导入数据和内置的 JSON 解析器构建了一个表。现在我试图让用户查看有关他们选择的标题的更多信息。此 api 的数据如下所示:

    [
      {
        "userId": 1,
        "id": 1,
        "title": "delectus aut autem",
        "completed": false
      },
      {
        "userId": 1,
        "id": 2,
        "title": "quis ut nam facilis et officia qui",
        "completed": false
      },
...cont...
]

现在我认为到达我想要的位置的方法是添加一个“cocoatouch 类”视图控制器,并向该文件中添加代码以更改标签字段。但是,我不确定要在这个名为“title_details_ViewController.swift”的 cocoatouch 类视图控制器中添加什么样的代码。我知道它必须以某种方式连接到我的名为“模型”的文件...

【问题讨论】:

    标签: ios swift api mvvm model


    【解决方案1】:

    对于 Detail 屏幕,就像你提到的,你需要一个像 DetailViewController 这样的 ViewController,你可以在 didSelectRowAt 方法中初始化 detailVC。

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
            let item = list[indexPath.row]
            
            let viewController(detailItem: item)
            navigationController?.pushViewController(viewController, animated: true)
        }
    

    您可以像下面这样定义您的详细信息屏幕

        DetailViewController: UIViewController {
    
        var detailItem: Item!
        
        convenience init(detailItem: Item) {
            self.init()
            self.detailItem = detailItem
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2016-06-25
      • 2014-12-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多