Swift 再次调用同一视图,以便在UITableViewController中选择另一个选项
背景: 这个简单的应用程序允许您从Swift 再次调用同一视图,以便在UITableViewController中选择另一个选项,swift,uitableview,Swift,Uitableview,背景: 这个简单的应用程序允许您从UITableViewController中选择一种货币,再次调用相同的视图进行第二次选择,然后将用户带到一个新视图,其中显示两种选定的货币和汇率 所以理论上对我来说,这只是两种观点。第一个是货币清单,第二个是列出选定的货币/汇率。第一个视图是完整的设计视图。但是,我正在为如何在第一个选择和第二个选择之间建立联系而苦苦挣扎,因为它调用了相同的观点。我该怎么做 在我的didSelectRowAt中,我通常会执行检查,但如何调用同一视图并记录从第一个视图中选择的值?
UITableViewController
中选择一种货币,再次调用相同的视图进行第二次选择,然后将用户带到一个新视图,其中显示两种选定的货币和汇率
所以理论上对我来说,这只是两种观点。第一个是货币清单,第二个是列出选定的货币/汇率。第一个视图是完整的设计视图。但是,我正在为如何在第一个选择和第二个选择之间建立联系而苦苦挣扎,因为它调用了相同的观点。我该怎么做
在我的didSelectRowAt
中,我通常会执行检查,但如何调用同一视图并记录从第一个视图中选择的值?我的一个想法是调用一个函数,该函数将记录是否选择了一个选项,如果选择了,则调用新视图,否则再次调用相同的视图,但我不确定如何实现这一点。感谢您的帮助
到目前为止,我的代码是:
import UIKit
class SelectCurrencyTableViewController: UITableViewController {
override func viewDidLoad() {
super.viewDidLoad()
}
// Get the JSON data to insert into the table
func parseJSONData()-> Array<Any> {
var finalArray = [Any]()
if let url = Bundle.main.url(forResource: "currencies", withExtension: "json") {
do {
let data = try Data(contentsOf: url)
let jsonResult = try JSONSerialization.jsonObject(with: data)
if var jsonArray = jsonResult as? [String] {
while jsonArray.count > 0 {
let result: [String] = Array(jsonArray.prefix(2))
finalArray.append(result)
jsonArray.removeFirst(2)
}
}
} catch {
print(error)
}
}
return finalArray
}
func checkOptionsCount()-> Int{
// somehow check if option selected?
return 1
}
// MARK: - Table view data source
override func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return parseJSONData().count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! TableViewCellController
if let array = parseJSONData()[indexPath.row] as? [String]{
cell.countryCodeLabel.text = array[0]
cell.currencyLabel.text = array[1]
cell.countryFlag.image = UIImage(named: array[0])
}
return cell
}
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
// if this is 1st time, present view again
if (checkOptionsCount() == 1){
// if this is 2nd time, show new view
} else if (checkOptionsCount() == 2){
// performSegue with new view
} else {
print("How did I get here")
}
}
/*
// MARK: - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
// Get the new view controller using segue.destination.
// Pass the selected object to the new view controller.
}
*/
}
导入UIKit
类SelectCurrencyTableViewController:UITableViewController{
重写func viewDidLoad(){
super.viewDidLoad()
}
//获取要插入到表中的JSON数据
func parseJSONData()->数组{
var finalArray=[Any]()
如果让url=Bundle.main.url(用于资源:“货币”,扩展名为:“json”){
做{
let data=try data(contentsOf:url)
让jsonResult=try JSONSerialization.jsonObject(with:data)
如果var jsonArray=jsonResult为?[String]{
而jsonArray.count>0{
let result:[String]=数组(jsonArray.prefix(2))
finalArray.append(结果)
jsonArray.removeFirst(2)
}
}
}抓住{
打印(错误)
}
}
返回最终射线
}
func checkoptionscont()->Int{
//以某种方式检查是否选择了该选项?
返回1
}
//标记:-表视图数据源
重写func numberOfSections(在tableView:UITableView中)->Int{
返回1
}
重写func tableView(tableView:UITableView,numberofrowsinssection:Int)->Int{
返回parseJSONData().count
}
重写func tableView(tableView:UITableView,cellForRowAt indexath:indexPath)->UITableViewCell{
让cell=tableView.dequeueReusableCell(标识符为“cell”,for:indexPath)作为!TableViewCellController
如果让数组=parseJSONData()[indepath.row]作为?[String]{
cell.countryCodeLabel.text=数组[0]
cell.currencyLabel.text=数组[1]
cell.countryFlag.image=UIImage(名称:数组[0])
}
返回单元
}
重写func tableView(tableView:UITableView,didSelectRowAt indexPath:indexPath){
//如果这是第一次,请再次显示视图
如果(CheckOptions计数()==1){
//如果这是第二次,则显示新视图
}else if(checkoptions计数()==2){
//以新的视角进行格调
}否则{
打印(“我是如何来到这里的”)
}
}
/*
//标记:-导航
//在基于故事板的应用程序中,您通常需要在导航之前做一些准备
覆盖功能准备(对于segue:UIStoryboardSegue,发送方:有吗?){
//使用segue.destination获取新的视图控制器。
//将选定对象传递给新的视图控制器。
}
*/
}
看到您的代码,我假设您正在使用故事板。实现您想要的目标的一种方法是:
class SelectCurrencyTableViewController: UITableViewController {
var selectedCurrency: Currency?
//...
}
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
// if this is 2nd time, show new view
if let selected = selectedCurrency {
// performSegue with new view
// if this is 1st time, present view again
// these is no selected currency passed from previous view controller, so this is the first time
} else {
//get view controller from storyboard using storyboard id (replace "Main" with your storyboard's name
let vc = UIStoryboard(name: "Main", bundle: nil)
.instantiateViewController(withIdentifier: "SelectCurrencyTableViewController") as! SelectCurrencyTableViewController
vc.selectedCurrency = //place code for getting first currency based on indexPath.row here
show(vc, sender: self)
}
}