【问题标题】:Pickerview not displaying选择器视图不显示
【发布时间】:2016-11-30 20:38:29
【问题描述】:

即使在重新加载所有组件后,由于某种原因,我的选择器视图也没有显示。我单击开始,它只是没有加载我希望加载的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
} 
}

【问题讨论】:

  • 如何创建选择器视图?你有没有在故事板中设置它?您还记得将委托链接连接到您的视图控制器吗?如果您不这样做,您的委托方法将不会被调用,并且选择器将为空。
  • 或者你也可以使用pickerView.delegate = self, pickerview.dataSource = self

标签: ios swift cocoa-touch uikit


【解决方案1】:

我的猜测:看起来只要numberOfRowsInComponent 被调用(一旦它准备添加到视图中它就会这样做)你也调用hide() 这反过来似乎将你的pickerView 设置为isHidden = true。我在您的代码中看不到任何地方 isHidden 随后设置为 false

如果情况并非如此,还要检查 pickerview.dataSource 的值以确保它不是 nil。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-30
    • 2016-03-22
    • 1970-01-01
    • 2018-02-26
    相关资源
    最近更新 更多