2016-06-01 55 views
1

斯威夫特不能觸摸按鍵這裏是我的問題在UIPickerView

我設置一個彈出pickerview並添加工具欄的頂部 但我不能觸摸或點擊UIPickerView的頂部按鈕

一切都OK了包括設置按鈕的PickerView和頂部

self.view.addSubview(pickerView) 
    self.pickerView.addSubview(toolBar) 

這兩個代碼是工作在我看來,可以選擇,但不能點擊完成按鈕

enter image description here

怎麼能點擊編碼?

我已經嘗試了 「userInteractionEnabled」 和 「willMoveToSuperview」 它不工作

PLZ有人能幫助我....感謝

所有的代碼是在這裏

import UIKit 

    class AddNew: UIViewController,UIPickerViewDelegate,UIPickerViewDataSource { 


    @IBOutlet weak var display: UILabel!  

    var pickerViewList = ["one","two","three","four","five"] 

    var pickerView = UIPickerView() 

    var toolBar = UIToolbar() 

    override func viewDidLoad(){ 
    super.viewDidLoad() 
    pickerView.delegate = self 
    pickerView.dataSource = self 
    pickerView.backgroundColor = UIColor.whiteColor() 

    toolBar.barStyle = UIBarStyle.Default 
    toolBar.backgroundColor = UIColor.whiteColor() 
    toolBar.translucent = true 
    toolBar.tintColor = UIColor.blueColor() 
    toolBar.sizeToFit() 

    let doneButton:UIBarButtonItem = UIBarButtonItem(title: "Done", style: UIBarButtonItemStyle.Done, target:self, action:#selector(AddNew.hideMypicker)) 

    let spaceButton = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.FlexibleSpace, target: nil, action: nil) 

    let cancelButton = UIBarButtonItem(title: "Cancel", style: UIBarButtonItemStyle.Plain, target: self, action:#selector(AddNew.hideMypicker)) 

    cancelButton.tintColor = UIColor.whiteColor() 
    toolBar.setItems([cancelButton, spaceButton, doneButton], animated: false) 
} 

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

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

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

func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) { 
    display.text = pickerViewList[row] 
} 

@IBAction func ClickButton(sender: UIButton) { 
    self.pickerView.frame.origin.y = self.view.frame.height 
    self.pickerView.bounds = CGRectMake(0, self.pickerView.bounds.origin.y, UIScreen.mainScreen().bounds.width, self.pickerView.bounds.height) 
    self.pickerView.frame.origin.x = 0 
    UIView.animateWithDuration(0.3){ 
     self.pickerView.frame.origin.y = self.view.frame.height - self.pickerView.frame.height 
    } 
    self.view.addSubview(pickerView) 
    self.pickerView.addSubview(toolBar) 
} 

func hideMypicker(){ 
    UIView.animateWithDuration(0.3,animations:{self.pickerView.frame.origin.y = self.view.frame.height},completion:{(value:Bool) in self.twofunction()}) 
} 



func twofunction() { 
    pickerView.removeFromSuperview() 
    toolBar.removeFromSuperview() 
} 
+0

請顯示toolBar和pickerView初始化代碼 –

+0

@ChathurangaSilva說了些什麼,如果按鈕的動作沒有正確連接,我會很懷疑。 – sschale

+0

Hare你的更多的代碼,使上下文變得更清晰線按鈕的動作是如何分配等 – dip

回答

0

何不你是否創建一個UIControl的自定義ActionSheetPickerView子類?您可以在那裏設置PickerViewDataSource和PickerViewDelegate方法,以便您可以在需要pickerView的地方使用相同的方法。

創建一個ActionSheetPickerViewDelegate(自定義委託),爲可選部分下的doneButtonTap定義方法。

該代碼將變得更清潔,更容易調試。現在回到你的代碼,你有沒有嘗試使用視圖調試器進行調試?