2017-02-18 45 views
0

我用UIPickerView爲標籤選擇數據:點擊UIPickerView

class BookingOptionsViewController: UIViewController, UIPickerViewDelegate, UIPickerViewDataSource { 

    @IBOutlet weak var lblProfile: UILabel! 

    var data = ["1", "2", "3"] 
    var picker = UIPickerView() 

    override func viewDidLoad() { 
     super.viewDidLoad()   
     picker.delegate = self 
     picker.dataSource = self 
     let tap = UITapGestureRecognizer(target: self, action: #selector(tap(gestureReconizer:))) 
     lblProfile.addGestureRecognizer(tap) 
     lblProfile.isUserInteractionEnabled = true 
    } 

    func tap(gestureReconizer: UITapGestureRecognizer) { 
     print("*") 
     picker.isHidden = false 
    } 

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

    func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int { 
     return data.count 
    }  

    func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) { 
     lblProfile.text = data[row] 
     self.view.endEditing(true) 
    } 

    func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? { 
     return data[row] 
    } 
} 

當我點擊標籤,我想表明我UIPickerView,但picker.isHidden = false不起作用。 我該怎麼做才能顯示UIPickerView

+0

@Krill你在哪裏隱藏你的選取器我的意思是picker.hidden = false是在那裏,但是picker.hidden = true在哪裏? –

回答

2

您從未完成選取器視圖的設置。你創建它。你設置它的委託和數據源,但就是這樣。你需要設置其框架。你需要將它添加到視圖控制器的視圖(或其他適當的父視圖)。

override func viewDidLoad() { 
    super.viewDidLoad()   

    var pickerRect = picker.frame 
    pickerRect.origin.x = // some desired value 
    pickerRect.origin.y = // some desired value 
    picker.frame = pickerRect 
    picker.delegate = self 
    picker.dataSource = self 
    picker.isHidden = true 
    view.addSubview(picker) 

    let tap = UITapGestureRecognizer(target: self, action: #selector(tap(gestureReconizer:))) 
    lblProfile.addGestureRecognizer(tap) 
    lblProfile.isUserInteractionEnabled = true 
} 
+0

@kirill你去那裏肯定會爲你工作。 –

+0

非常感謝 – Kirill

相關問題