2016-11-30 2 views
0

すべてのコンポーネントを再読み込みしても、ピッカービューが何らかの理由で表示されません。私は開始をクリックして、これをロードしたいときに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

ピッカービューの作成方法を教えてください。あなたはそれをストーリーボードに設定しましたか?デリゲートリンクをView Controllerに接続するのを忘れましたか?そうしないと、デリゲートメソッドが呼び出されず、ピッカーが空になります。 –

+0

または単に 'pickerView.delegate = self、pickerview.dataSource = self'を使うこともできます –

答えて

0

私の推測:それはできるだけ早くあなたにもターンであなたのpickerViewisHidden = trueに設定する思われるhide()を呼び出す(それは、すぐにそれがビューに追加する準備をしている)と呼ばれているnumberOfRowsInComponentとしてのように見えます。あなたのコードのどこにでも表示されません。isHiddenは、その後falseに設定されます。

そうでない場合は、pickerview.dataSourceの値がnilでないことを確認することも確認してください。

関連する問題