动态地向 UIBarButtonItem 添加操作 Swift 4

Add action to UIBarButtonItem dynamically Swift 4

let doneButton = UIBarButtonItem(title: "Done", style: UIBarButtonItemStyle.plain, target: self, action: #selector(buttonClicked(sender:)))

@objc func buttonClicked(sender: UIBarButtonItem) {
    print("Hello")
}

这是我的 UIBarButton 的代码,但是当我点击它时它不打印 "Hello",可能是什么问题?

编辑:这是我的 viewcontroller 它只是控制一个按钮,当它被点击时显示微调器及其控件,但正如我之前所说,工具栏上的按钮不起作用

class FilterViewController: UIViewController {

var search: Search?

let categoriesSpinnerDelegate = CategoriesPickerDelegate()

@IBOutlet weak var generalSpinner: UIPickerView!
@IBOutlet weak var categoriesButton: UIButton!
@IBOutlet weak var categoryRow: UIView!

var doneButton: UIBarButtonItem?

override func viewDidLoad() {
    super.viewDidLoad()

    self.navigationController?.topViewController?.title = "Filtri"

    // Set border and click action
    self.categoryRow.layer.borderWidth = 1
    self.categoryRow.layer.borderColor = Raccoltacase.lightGray.cgColor
    self.categoryRow.addGestureRecognizer(UIPanGestureRecognizer(target: self, action: #selector(self.buttonClicked(sender:))))

    // Create toolbar and attach it to pickerView
    let toolBar = UIToolbar()
    toolBar.barStyle = UIBarStyle.default
    toolBar.isTranslucent = true
    //toolBar.tintColor = UIColor(red: 76/255, green: 217/255, blue: 100/255, alpha: 1)
    toolBar.sizeToFit()

    self.doneButton = UIBarButtonItem(title: "Done", style: UIBarButtonItemStyle.plain, target: self, action: #selector(buttonClicked(sender:)))
    let spaceButton = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.flexibleSpace, target: nil, action: nil)
    let cancelButton = UIBarButtonItem(title: "Cancel", style: UIBarButtonItemStyle.plain, target: self, action: nil)

    toolBar.setItems([cancelButton, spaceButton, doneButton!], animated: false)
    toolBar.isUserInteractionEnabled = true

    generalSpinner.addSubview(toolBar)

}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

@IBAction func categoriesButtonClick(_ sender: UIButton) {
    self.generalSpinner.showsSelectionIndicator = true
    self.generalSpinner.dataSource = categoriesSpinnerDelegate
    self.generalSpinner.delegate = categoriesSpinnerDelegate
    self.generalSpinner.isHidden = false
}

@objc func buttonClicked(sender: UIBarButtonItem) {
    print("Hello")
}
}

Screen

假设您有时会显示选择器(并在用户按下完成按钮时将其关闭),这是我的解决方案:

  1. UITextField 添加到情节提要中的视图(并使 tintColor 透明 (clearColor))

  2. UIToolbar 作为 inputAccessoryView 添加到 UITextField

  3. UIPickerView 作为 inputView 添加到工具栏(另见下面的注释)

下面是示例代码:

override func viewDidLoad() {
    super.viewDidLoad()
    textField.inputView = generalSpinner
    textField.inputAccessoryView = getToolbar()
}

func getToolbar() -> UIToolbar {
    let toolBar = UIToolbar(frame:         CGRect(x: 0, y: 0, width: self.view.frame.width, height: 40))
    toolBar.barStyle = UIBarStyle.default
    toolBar.isTranslucent = true
    self.doneButton = UIBarButtonItem(title: "Done", style: UIBarButtonItemStyle.plain, target: self, action: #selector(buttonClicked(sender:)))
    let spaceButton = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.flexibleSpace, target: nil, action: nil)
    let cancelButton = UIBarButtonItem(title: "Cancel", style: UIBarButtonItemStyle.plain, target: self, action: nil)

    toolBar.setItems([cancelButton, spaceButton, doneButton!], animated: false)
    toolBar.isUserInteractionEnabled = true

    return toolBar
}

注意:如果 UIPickerView 已经在故事板中(或另一个视图的子视图),请确保在 viewDidLoad 的第一行将其删除(如下所示) ):

override func viewDidLoad() {
  super.viewDidLoad()
  generalSpinner.removeFromSuperview()
  textField.inputView = generalSpinner
  textField.inputAccessoryView = getToolbar()
}

已解决 当我使用 pickerView.addSubView(toolbar) 时,工具栏位于 pickerview 后面,因此无法点击。我用 General Spinner.top = Picker Toolbar.bottom

解决了从情节提要中手动添加工具栏的问题