2016-11-30 48 views
0

即使在重新加载所有组件后,我的pickerview也不会显示。我点击开始,它只是不加载pickerview,因为我想这加载,然后你可以选择一个选项,并转到下一个问题?Pickerview不显示

import UIKit 

class QuestionsViewController: UIViewController, UIPickerViewDelegate { 

@IBOutlet weak var Next: UIButton! 
@IBOutlet weak var pickerview: UIPickerView! 
@IBOutlet weak var itemLabel: UILabel! 
@IBOutlet weak var label1: UILabel! 
@IBOutlet weak var Question: UILabel! 


@IBAction func Next(_ sender: Any) { 
    cQuestion.currentQuestion = cQuestion.currentQuestion + 1 
    pickerview.reloadAllComponents() 


} 

override func didReceiveMemoryWarning() { 
    super.didReceiveMemoryWarning() 
    // Dispose of any resources that can be recreated. 
} 
//var LabelText: String = "" 
//var arrayOfQuestions: [String] = ["&","&&","||","None of above"] 
//var correctAns:String = "" 
//var currentQuestion = 0 

let cQuestion = Questions() 


//init(arrayOfQuestions:String, correctAns:String, LabelText:String) { 
    //self.arrayOfQuestions = [arrayOfQuestions] 
    // self.correctAns = correctAns 
    // self.LabelText = LabelText 
//} 




override func viewDidLoad() { 
    super.viewDidLoad() 

    // Do any additional setup after loading the view, typically from a nib. 

    itemLabel.text = cQuestion.arrayOfQuestions[0] 
} 

func numberOfComponents(in pickerView: UIPickerView) -> Int { 

    return 1 

} 

func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int{ 

    if(cQuestion.currentQuestion == 0) { 
     Question.text = "Q1. Which is a logical OR operator" 
     return cQuestion.arrayOfQuestions.count 
    } else if (cQuestion.currentQuestion == 1) { 
     Question.text = "Q2. Compiler generates_file" 
     return cQuestion.arrayOfQuestions.count 
    } 
    hide() 
    Question.text = "You have finished" 
    Next.isHidden = true 
    return cQuestion.arrayOfQuestions.count 

} 


func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String?{ 

    if(cQuestion.currentQuestion == 0) { 
     return cQuestion.arrayOfQuestions[row] 
    } else if (cQuestion.currentQuestion == 1) { 
     return cQuestion.arrayOfQuestions[row] 
    } 
    return cQuestion.arrayOfQuestions[row] 

} 


func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int){ 

    if (cQuestion.currentQuestion == 0) { 
     let itemSelected = cQuestion.arrayOfQuestions[row] 
     itemLabel.text = itemSelected 
    } else if (cQuestion.currentQuestion == 1) { 
     let itemSelected = cQuestion.arrayOfQuestions[row] 
     itemLabel.text = itemSelected 
    } 
} 

func hide() { 
    pickerview.isHidden = true 
    itemLabel.isHidden = true 
} 
} 
+1

如何创建选择器视图?你有没有在故事板中设置?你记得把委托链接连接到你的视图控制器吗?如果你不这样做,你的委托方法将不会被调用,并且选择器将是空的。 –

+0

或者你也可以使用'pickerView.delegate = self,pickerview.dataSource = self' –

回答

0

我的猜测:它只要numberOfRowsInComponent被称为看起来你还叫hide()这又似乎设置您pickerViewisHidden = true(它,因为它准备被添加到视图尽快做)。我没有看到代码中的任何地方,其中isHidden随后被设置为false

如果情况不是这样,请检查pickerview.dataSource的值是否确定它不为零。