pickerview.reloadAllComponents()无效

时间:2016-12-01 13:39:44

标签: ios xcode

我在" pickerview.reloadAllComponents()"中遇到错误我无法进入第二个问题,因为有一个错误表明

  

致命错误:在解包可选值时意外发现nil

import UIKit

class QuestionsViewController: UIViewController, UIPickerViewDelegate {


    @IBOutlet weak var Next: UIButton!
    @IBOutlet weak var itemLabel: UILabel!
    @IBOutlet weak var label1: UILabel!
    @IBOutlet weak var Question: UILabel!
    @IBOutlet weak var pickerview: UIPickerView!
    let cQuestion = Questions()
    public var q1: [String] = ["&","&&","||","None of above"]
    public var q2: [String] = ["&","&&","||","hello"]



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


    }




    override func viewDidLoad() {
        label1.text = cQuestion.LabelText


        super.viewDidLoad()



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

        itemLabel.text = cQuestion.q1[0]

    }

    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{
        var qcount: Int = 0

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

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

    }


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

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

    }


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

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

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


}

1 个答案:

答案 0 :(得分:0)

我认为您的{pickerview IBOutlet未正确连接,请确保您需要办理登机手续StoryboardXib pickerview是否正确连接?

我希望它对你有用