简体   繁体   English

PickerView上的UIBarButton不能启动

[英]UIBarButton on PickerView doesn't wok

What I want to do is to get an action evnet from the bar button. 我想要做的是从工具栏按钮获得一个动作evnet。 At the beginning, I put the bar button on the bar tool on on pickerview. 一开始,我将bar按钮放在pickerview上的bar工具上。 The button display on the pickerview, but I couldn't push the button. 该按钮显示在pickerview上,但是我无法按下按钮。 The color of the button doesn't change at all. 按钮的颜色完全不变。 It means I cannot touch the button. 这意味着我无法触摸按钮。 The pickerview can be scrolled even on the button area. 甚至可以在按钮区域上滚动pickerview。 It seems like the button is under the pickerview. 似乎该按钮在pickerview下。 Once I add the tool bar with slef.view.adSubView directly on the view, I could push the bar button and get the action. 一旦在视图上直接添加了带有slef.view.adSubView的工具栏,就可以按下工具栏按钮并执行操作。 I want to get an action from the bar button on the picker view. 我想从选择器视图上的条形按钮中获取操作。 I don't use these UIs on main story board for this time. 我这次不在主要故事板上使用这些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]
}

I use Xcode 7.1. 我使用Xcode 7.1。

The first time you was not able to push the button may be due to UserInteractionEnabled was not selected in xib. 第一次无法按下按钮的原因可能是由于未在xib中选择UserInteractionEnabled

Anyways, please try this to add programmatically: 无论如何,请尝试通过编程添加以下内容:

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

func pushBarButton(){ pickerView1.hidden = true }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM