简体   繁体   English

我可以使用swift3 iOS在一个UIViewcotroller中设置ModelClass并从另一个UIViewCotroller中读取ModelClass的对象吗?

[英]Can I set a ModelClass in one UIViewcotroller and read the ModelClass' object from another UIViewCotroller using swift3 iOS?

I'm trying to learn to make use of ClassModels in swift3, since I couldn't find a proper tutorial to understand the basic concept, I referred to too many sites and succeeded in creating a model class and setting values to the model class' objects from a sample parsed JSON value. 我试图在swift3中学习使用ClassModels,因为找不到合适的教程来理解基本概念,因此我引用了太多站点,并成功创建了模型类并为模型类设置值”样本解析的JSON值中的对象。

My model class is, 我的模特班是

import UIKit
import SwiftyJSON

class TrainObject {


    var towards: String!
    var lastUpdated: String!
    var delay: String!
    var trainName: String!
    var lastLocation: String!
    var trainNo: String!
    var upcomingStationInfo: String!


    required init(json: JSON) {

        lastUpdated = json["lastUpdated"].stringValue
        trainName = json["trainName"].stringValue        

    }

}

Now from ListViewController(UIViewController)'s viewdidload, I parsed JSON from a URL using Alamofire and set the resultant to the model class using the code, 现在,从ListViewController(UIViewController)的viewdidload中,我使用Alamofire从URL解析JSON,然后使用代码将结果设置为模型类,

 if let results = json["info"]["trainData"].array {
            print(results)
            for entry in results {
                self.items.append(TrainObject(json: entry))
            }
            print(self.items)


            let user = self.items[0]
            print(user.trainName)
            self.tableOfInfo?.reloadData()
        }

Further from the ListViewController's tableview delegate, I got the array values one by one to be loaded in the tableview like, 远离ListViewController的tableview委托,我将数组值一个接一个地加载到tableview中,例如,

            let user = self.items[indexPath.row]
            print(user.trainName)
            print(user.delay)

Now on tableview selection, I can pass appropriate value to another UIViewController named DetailsViewController using the usual code. 现在,在选择tableview时,我可以使用通常的代码将适当的值传递给另一个名为DetailsViewController的UIViewController。 My question is, is there a way to access the model class(TrainObject) directly from the DetailsViewController and then with the selected indexpath.row value, obtain the appropriate array? 我的问题是,是否有一种方法可以直接从DetailsViewController访问模型类(TrainObject),然后使用选定的indexpath.row值来获取适当的数组? or should I pass the array itself from tableview's didselectrow method? 还是应该通过tableview的didselectrow方法传递数组本身? I get confused here Can you please explain what's the real use of model class? 我在这里感到困惑,能否请您解释一下模型类的真正用途是什么? How and where should I actually use it? 我应该在哪里实际使用它?

You should fetch the item from array when user select a particular item on the tableview . 当用户在tableview选择特定项目时,应从数组中获取该项目。 And get the instance of the DetailViewController and pass the selected item to that instance. 并获取DetailViewController的实例并将所选项目传递给该实例。

For Example : 例如 :

class DetailViewController: UIViewController {

   var selectedTrain : TrainObject?

  override func viewDidLoad() { 
    if let selectedTrain = selectedTrain {
       // Selected train not nil.
    }
    super.viewDidLoad()
  }


}

class ListViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {


    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        let selectedTrain = self.items[indexPath.row]

        let detailViewController = // Fetch the instance from Storyboard 

        detailViewController.selectedTrain = selectedTrain

        //Push the detail view controller or present it modally. 

    }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在Swift中访问AppDelegate中的UIViewCotroller - Access a UIViewCotroller in AppDelegate in Swift 如何从iPhone SDK中的UIViewCotroller绘制矩形 - How to draw a rectangle from UIViewCotroller in iPhone sdk 内存管理问题(发布UIVIewCotroller) - Memory management problem (release UIVIewCotroller) 如何推送清晰或 alpha UIViewCotroller - How to Push a clear or alpha UIViewCotroller 我如何使用swift3从Firebase数据库中存储具有多个字符串的对象 - How can I store object has a multiple strings from firebase database using swift3 如何在swift3 ios中使用键盘返回键将一个文本字段移动到另一个文本字段 - How to move one text field to another using keyboard return key in swift3 ios ModelClass数据传递 - ModelClass Data passing 我如何在swift3的appleTvOS开发项目中将焦点从一个对象更改为我想要的下一个首选对象 - How can i change focus from one object to next preferred object which i want in appleTvOS development project in swift3 我如何使用swift在不是当前的视图控制器中显示从数据库中检索到的数据,而在ios中显示另一个 - how can i show retrieved data from database in not a present view controller but another one in ios with using swift 在Swift3 iOS中使用字符串参数对对象数组进行排序 - Sort an array of object using string parameter in Swift3 iOS
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM