Swift 无法触摸 UIPickerView 中的按钮

标签 swift button uipickerview toolbar

这是我的问题

我设置了一个弹出式选择器 View 并在顶部添加了工具栏 但我无法触摸或单击 UIPickerView 顶部的按钮

一切正常,包括设置 PickerView 和 Button 顶部

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

这两个代码在我看来是有效的,可以选择但不能点击完成按钮

enter image description here

如何编码才能点击?

我试过“userInteractionEnabled”和“willMoveToSuperview” 它不起作用

请有人帮助我....谢谢

所有代码在这里

    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()
}

最佳答案

为什么不创建自定义 ActionSheetPickerView 的 UIControl 子类?您可以在那里设置 PickerViewDataSource 和 PickerViewDelegate 方法,以便您可以在需要 pickerView 的任何地方使用相同的方法。

创建一个 ActionSheetPickerViewDelegate(自定义委托(delegate)),在可选部分下定义 doneButtonTap 的方法。

代码会变得更清晰,也更容易调试。现在回到您的代码,您是否尝试过使用 View 调试器进行调试??

关于Swift 无法触摸 UIPickerView 中的按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37559522/

相关文章:

ios - 在 Swift 中从 UIView 上的特定点开始淡入淡出动画

HTML 按钮边框

xcode - 比较 iOS 8 中的 ImageView 不起作用

c# - WPF Material Design 按钮悬停

ios - 不符合协议(protocol) UIPickerViewDataSource

ios - 如何通过for循环加载选择器 View ?

swift - 如何返回此测试中可观察量的下一个值?

arrays - 阵列平均速度和最高速度 "CoreLocation"

ios - UIPickerView 在我打开时崩溃?

ios - 与单 View 应用程序相比,具有 UICollectionView 的今日扩展具有不同的行为