数组中的数据未显示在第二个 UIPickerView 中
Posted
技术标签:
【中文标题】数组中的数据未显示在第二个 UIPickerView 中【英文标题】:Data in array not showing up in second UIPickerView 【发布时间】:2017-07-27 16:18:16 【问题描述】:我是 ios 开发的新手,所以请记住我的问题。我正在开发一个将显示初始数组的应用程序。根据数组的选择,第二个pickerview(显示在下一个视图控制器中)将使用字典和第一个pickerview中的选定选项来使用子主题列表填充第二个pickerview。控制台显示所有数据都正确地通过了该过程,但是数据没有填充到第二个选择器视图中。有人可以看看我的代码并告诉我哪里出错了吗?
import UIKit
import Foundation
class ViewController: UIViewController, UIPickerViewDelegate, UIPickerViewDataSource
@IBOutlet weak var pickerView: UIPickerView!
@IBAction func SubjectSelected(_ sender: UIButton, forEvent event: UIEvent)
SubjectHandler = subSubjects[SelectedSubject]!
print(SubjectHandler)
print(Subjects)
//Create instances of the selected subject & the subject handler (in case SelectedSubject is not empty)
var SelectedSubject: String = ""
var SubjectHandler: Array<String> = []
//Define primary values for both subjects and subSubjects//
let Subjects: Array = ["Macroeconomics", "Microeconomics", "Financial Economics", "Game Theory", "Econometrics", "Law Economics", "Public Sector Economics", "International Economics", "General Statistics"]
let subSubjects = [
"Macroeconomics":
["GDP", "Accounting Methods", "Labor Market", "DMP Job Search Model", "Keynesian Economics", "Sticky-Price Model", "Elasitcity", "Supply and Demand"],
"Microeconomics":
["Elasticity", "Consumer Theory", "Preference Curves", "Competitive Models", "Scarcity"],
"Financial Economics":
["Interest", "Dividends Returns", "Return & Expected Return"],
"Game Theory":
["Nash Equilibrium", "Dominant Strategies", "Iterated Games", "Backwards Induction", "Extensive Form Games"],
"Econometrics":
["Capital Asset Pricing Model", "Regressional Analysis", "Modeling Rules", "Central Limit Theorem", "Probablitiy & Distribution","Heteroscedasticity", "Weighted Least Squares", "Sampling Distributions"],
"Law Economics":
["Externalities", "Damages Calculations", "Property Rights Ownership", "Claims", "Ownership Principles"],
"Public Sector Economics": ["Valuations", "Tax Income Calculations", "Budget Analysis"], "International Economics": ["Taxing Methods", "Importing and Exporting", "Currency Exchange Rates", "Tarrifs", "GDP", "Product Purchase Parity"],
"General Statistics":
["Z-Scores", "Summary Statistics", "One-Tailed Hypothesis Testing", "Two-Tailed Hypothesis Testing", "Confidence Intervals", "Upper and Lower Bounds"]
]
override func viewDidLoad()
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
pickerView.delegate = self
pickerView.dataSource = self
if pickerView.tag == 2
pickerView.reloadAllComponents()
override func didReceiveMemoryWarning()
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
func numberOfComponents(in pickerView: UIPickerView) -> Int
return 1
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int
if pickerView.tag == 1
return Subjects.count
else
return SubjectHandler.count
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String?
if pickerView.tag == 1
return Subjects[row]
else
return SubjectHandler[row]
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int)
SelectedSubject = Subjects[row]
【问题讨论】:
【参考方案1】:正如您所说,您在第二个屏幕上使用了一个单独的 viewController 对象。当它被加载时,它将有一个空的SubjectHandler
数组。
你应该在 viewController 之间传递数组。见this tutorial
【讨论】:
谢谢,我没有意识到每个屏幕都需要单独的视图控制器! 只是想跟进并让您知道您的回答帮助我解决了这个问题....几天来我一直在努力解决这个问题!再次感谢您 很高兴为您提供帮助。祝你的应用好运!以上是关于数组中的数据未显示在第二个 UIPickerView 中的主要内容,如果未能解决你的问题,请参考以下文章
我如何在 android 中显示从 listView 到第二个活动的相同数据,如果我点击 google,它会在第二个活动中显示 google