PickerView上的UIBarButton没有炒锅

时间:2015-12-27 21:26:07

标签: ios iphone xcode swift

我想要做的是从条形按钮获取动作evnet。开始时,我将条形工具上的条形按钮放在pickerview上。选择器视图上的按钮显示,但我无法按下按钮。按钮的颜色根本不会改变。这意味着我无法触摸按钮。即使在按钮区域,也可以滚动选择器视图。看起来这个按钮位于pickerview之下。  一旦我在视图上直接添加带有slef.view.adSubView的工具栏,我就可以按下栏按钮并获取操作。我想从选择器视图上的条形按钮获取操作。我这次不在主要故事板上使用这些UI。

var pickerView1: UIPickerView = UIPickerView()
var toolBar1 = UIToolbar()
var barButton1 = UIBarButtonItem()
let strData: [String] = ["abc","xyz"]

override func viewDidLoad() {
    super.viewDidLoad()

    // init bar button
    barButton1 = UIBarButtonItem(title: "PUSH", style: UIBarButtonItemStyle.Plain, target: self, action: Selector("pushBarButton:"))
    barButton1.enabled = true
    // init tool bar
    toolBar1.barStyle = UIBarStyle.Default
    toolBar1.translucent = true
    toolBar1.userInteractionEnabled = true
    toolBar1.sizeToFit()
    toolBar1.delegate = self
    // init picker
    pickerView1.delegate = self
    pickerView1.frame = CGRectMake(0.0, (self.view.bounds.height/3)*2, self.view.bounds.width , self.view.bounds.height / 3.0 )

    // add button on tool bar
    toolBar1.setItems([barButton1], animated: true)
    // add tool bar on picker
    pickerView1.addSubview(toolBar1)
    // add picker on view
    self.view.addSubview(pickerView1)
}

func pushBarButton( sender: AnyObject){
    pickerView1.hidden = true
}
func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int {
    return 1
}
func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
   return strData.count
}
func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
    return strData[row]
}

我使用Xcode 7.1。

1 个答案:

答案 0 :(得分:0)

第一次无法按下按钮可能是由于未在xib中选择UserInteractionEnabled

无论如何,请尝试以编程方式添加:

barButton1 = UIBarButtonItem(title:"PUSH",style:UIBarButtonItemStyle.Plain,target:self,action: Selector("pushBarButton"))

func pushBarButton(){ pickerView1.hidden = true }