2017-07-27 32 views
0

我是一個IOS開發的新手,所以請記住我的問題。我正在研究一個將顯示初始數組的應用程序。根據數組的選擇,第二個pickerview(在下一個視圖控制器中顯示)將使用字典,並從第一個選擇器視圖中選擇選項,以第二個選擇器視圖填充子主題列表。控制檯顯示所有數據在整個過程中都正確傳遞,但數據不在第二個選擇器視圖中填充。有人可以看看我的代碼並告訴我哪裏出錯了嗎?數組中沒有顯示的數據UIPickerView

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

如上所述,您在第二個屏幕上使用單獨的viewController對象。當它被加載時,它將有一個空的SubjectHandler陣列。

你可以在viewControllers之間傳遞數組。見this tutorial

+0

謝謝,我沒有意識到你需要單獨的viewControllers爲每個屏幕! – JamesSkinner12

+0

只是想跟進,讓你知道你的答案幫助我解決了這個問題....我在牆上撞了我的頭幾天試圖找出一個!再次感謝你 – JamesSkinner12

+0

很高興提供幫助。祝你的程序好運! –