我是IOS开发的新手,所以请记住我的问题。我正在开发一个显示初始数组的应用程序。根据数组的选择,第二个选择器视图(显示在下一个视图控制器中)将使用字典和第一个选择器视图中的选定选项,用子主题列表填充第二个选择器视图。控制台显示所有数据正在通过该过程正确传递,但数据未在第二个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]
}
}
答案 0 :(得分:0)
正如您所说,您在第二个屏幕上使用单独的viewController对象。当它加载时,它将有一个空的SubjectHandler
数组。
你可以在viewControllers之间传递数组。见this tutorial