Pickerview没有显示

时间:2016-11-30 20:38:30

标签: ios swift cocoa-touch uikit

即使重新加载所有组件,我的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 :(得分:0)

我的猜测:看起来只要numberOfRowsInComponent被调用(一旦它准备添加到视图中就会这样做)你也会调用hide(),而pickerView依次设置你的isHidden = true isHiddenfalse。我在代码中的任何位置都看不到pickerview.dataSource随后被设置为set backspace=indent,eol,start

如果情况不是这样,请检查backspace的值是什么,以确保它不是零。