如何将二维数组传递给 viewcontroller?
How do I pass 2D array to the viewcontroller?
我正在解码结果,我想将名称和 ID 发送到 viewcontroller。
我刚开始学习 JSON Codeable
那么,我如何传递这些数据,我已经将这 2 个变量声明为一个数组。
var cityNames = [String]()
var cityNameId = [Int]()
for cityName in display.cities{
print(cityName.id , cityName.name)
self.cityNames.append(cityName.name)
self.cityNameId.append(cityName.id)
}
打印时我得到了输出
1 Mumbai
2 Pune
3 Thane
预期结果I'm want to display the names of the cities to the label in dropdown list of next viewController and after selecting the city name from drop down ,. I need the ID of that selected city for the button click event
不要那样做。
显然 cities
表示包含 name
和 id
信息的对象数组
删除数组
var cityNames = [String]()
var cityNameId = [Int]()
始终传递结构的实例或 class 例如
if let firstCity = display.cities.first {
print(firstCity.id , firstCity.name)
// pass firstCity to next view controller
}
或
if let city = display.cities.first(where: {[=12=].name == "New York"}) {
print(city.id , city.name)
// pass city to next view controller
}
或在 table 视图的 didSelectRow
中
let city = display.cities[indexPath.row]
// pass city to next view controller
声明下一个视图控制器的引用(根据您的要求更改viewcontroller/storyboard/identifiers的名称):
let nextVC = NextViewController()
或者如果您使用的是故事板,您也可以从故事板实例化:
let nextVC = UIStoryboard(name: "Home", bundle: nil).instantiateViewController(withIdentifier: "NextViewController") as! NextViewController
现在将您想要的任何值作为二维数组传递给 nextVC:
let cities2DArray = [cityNameId, cityNames] as [Any]
nextVC.cities2DArray = cities2DArray
如果您使用带有标识符的 segue,则可以在 prepare(for segue: UIStoryboardSegue, sender: Any?)
中完成上述所有工作,如果没有,则只需在以编程方式进入 nextVC 之前完成这些工作。
现在,确保在 NextViewController
:
中声明了这些 2DArray 变量
class NextViewController: UIViewController {
var cities2DArray = [Any]()
更新:
如果您确实想在元组内的 1 个数组中并排创建一个具有 id 及其相应名称的元组,则需要按以下方式修改 for 循环:
var citiesTuple = [(id: Int, name: String)]()
for cityName in display.cities{
citiesTuple.append((id: cityName.id, name: cityName.name))
}
首先为 City 创建一个结构为 Class
或 Struct
class Context {
let cities: [City]
init(with cities: [City]) {
self.cities = cities
}
}
比在目标视图控制器中将变量描述为 Context,
class TargetViewController: UIViewController{
...
var context: Context?
}
然后,如果您手动推送 viewController,
var targetController = TargetViewController()
targetController.context = Context(with: self.cities)
navigationController?.pushViewController(with: targetController)
或者您可以使用 prepareSegue
方法从情节提要中设置它。
override func prepareForSegue(segue: UIStoryboardSegue!, sender: AnyObject!) {
if (segue.identifier == "TargetSegueIdentifier") {
var targetController = segue.destination as? TargetViewController
targetController.context = Context(with: self.cities)
}
}
我正在解码结果,我想将名称和 ID 发送到 viewcontroller。 我刚开始学习 JSON Codeable 那么,我如何传递这些数据,我已经将这 2 个变量声明为一个数组。
var cityNames = [String]()
var cityNameId = [Int]()
for cityName in display.cities{
print(cityName.id , cityName.name)
self.cityNames.append(cityName.name)
self.cityNameId.append(cityName.id)
}
打印时我得到了输出
1 Mumbai
2 Pune
3 Thane
预期结果I'm want to display the names of the cities to the label in dropdown list of next viewController and after selecting the city name from drop down ,. I need the ID of that selected city for the button click event
不要那样做。
显然 cities
表示包含 name
和 id
信息的对象数组
删除数组
var cityNames = [String]()
var cityNameId = [Int]()
始终传递结构的实例或 class 例如
if let firstCity = display.cities.first {
print(firstCity.id , firstCity.name)
// pass firstCity to next view controller
}
或
if let city = display.cities.first(where: {[=12=].name == "New York"}) {
print(city.id , city.name)
// pass city to next view controller
}
或在 table 视图的 didSelectRow
中
let city = display.cities[indexPath.row]
// pass city to next view controller
声明下一个视图控制器的引用(根据您的要求更改viewcontroller/storyboard/identifiers的名称):
let nextVC = NextViewController()
或者如果您使用的是故事板,您也可以从故事板实例化:
let nextVC = UIStoryboard(name: "Home", bundle: nil).instantiateViewController(withIdentifier: "NextViewController") as! NextViewController
现在将您想要的任何值作为二维数组传递给 nextVC:
let cities2DArray = [cityNameId, cityNames] as [Any]
nextVC.cities2DArray = cities2DArray
如果您使用带有标识符的 segue,则可以在 prepare(for segue: UIStoryboardSegue, sender: Any?)
中完成上述所有工作,如果没有,则只需在以编程方式进入 nextVC 之前完成这些工作。
现在,确保在 NextViewController
:
class NextViewController: UIViewController {
var cities2DArray = [Any]()
更新: 如果您确实想在元组内的 1 个数组中并排创建一个具有 id 及其相应名称的元组,则需要按以下方式修改 for 循环:
var citiesTuple = [(id: Int, name: String)]()
for cityName in display.cities{
citiesTuple.append((id: cityName.id, name: cityName.name))
}
首先为 City 创建一个结构为 Class
或 Struct
class Context {
let cities: [City]
init(with cities: [City]) {
self.cities = cities
}
}
比在目标视图控制器中将变量描述为 Context,
class TargetViewController: UIViewController{
...
var context: Context?
}
然后,如果您手动推送 viewController,
var targetController = TargetViewController()
targetController.context = Context(with: self.cities)
navigationController?.pushViewController(with: targetController)
或者您可以使用 prepareSegue
方法从情节提要中设置它。
override func prepareForSegue(segue: UIStoryboardSegue!, sender: AnyObject!) {
if (segue.identifier == "TargetSegueIdentifier") {
var targetController = segue.destination as? TargetViewController
targetController.context = Context(with: self.cities)
}
}