[英]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.