下面的屏幕有两个按钮的工具栏。他们有处理程序。处理程序不会从按钮触发。我尝试将处理程序分配给红色按钮,它可以工作。我需要帮助才能找到错误
以下是我的代码:
let toolbar: UIToolbar = {
let tb = UIToolbar(frame: CGRect(x: 0, y: 0, width: 10, height: 10))
tb.isUserInteractionEnabled = true
let doneButton = UIBarButtonItem(barButtonSystemItem: .done, target: self, action: #selector(choosen))
let cancelButton = UIBarButtonItem(barButtonSystemItem: .cancel, target: self, action: #selector(cancelSelection))
tb.items = [doneButton, cancelButton]
tb.translatesAutoresizingMaskIntoConstraints = false
return tb
}()
override func viewDidLoad() {
super.viewDidLoad()
let but = UIButton(frame: CGRect(x: 100, y: 100, width: 100, height: 100))
but.addTarget(self, action: #selector(cancelSelection), for: .touchUpInside)
but.backgroundColor = .red
self.view.addSubview(but)
self.view.obscure()
weightPicker.addSubview(toolbar)
toolbar.leadingAnchor.constraint(equalTo: weightPicker.leadingAnchor).isActive = true
toolbar.trailingAnchor.constraint(equalTo: weightPicker.trailingAnchor).isActive = true
toolbar.heightAnchor.constraint(equalToConstant: 50).isActive = true
toolbar.topAnchor.constraint(equalTo: weightPicker.topAnchor).isActive = true
self.view.addSubview(weightPicker)
weightPicker.delegate = self
weightPicker.leadingAnchor.constraint(equalTo: self.view.leadingAnchor).isActive = true
weightPicker.trailingAnchor.constraint(equalTo: self.view.trailingAnchor).isActive = true
weightPicker.heightAnchor.constraint(equalTo: self.view.heightAnchor, multiplier: 0.5).isActive = true
weightPicker.bottomAnchor.constraint(equalTo: self.view.bottomAnchor).isActive = true
}
@objc func choosen() {
print(123)
self.dismiss(animated: true, completion: nil)
}
@objc func cancelSelection() {
print(123)
self.dismiss(animated: true, completion: nil)
}
答案 0 :(得分:0)
您好,在操作中尝试在功能名#selector('here'.cancelSelection)
之前添加控制器类的名称或执行此操作#selector(self.cancelSelection)
答案 1 :(得分:0)
尝试使用懒惰我认为问题是因为你使用自我
lazy var toolbar: UIToolbar = {
let tb = UIToolbar(frame: CGRect(x: 0, y: 0, width: 10, height: 10))
tb.isUserInteractionEnabled = true
let doneButton = UIBarButtonItem(barButtonSystemItem: .done, target: self, action: #selector(self.choosen))
let cancelButton = UIBarButtonItem(barButtonSystemItem: .cancel, target: self, action: #selector(self.cancelSelection))
tb.items = [doneButton, cancelButton]
tb.translatesAutoresizingMaskIntoConstraints = false
return tb}()
答案 2 :(得分:0)
将UIToolbar添加到UIPickerView时似乎出错了。您可以改为添加一些容器视图:
override func viewDidLoad() {
super.viewDidLoad()
let containerView = UIView()
containerView.translatesAutoresizingMaskIntoConstraints = false
weightPicker.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(containerView)
containerView.addSubview(toolbar)
containerView.addSubview(weightPicker)
NSLayoutConstraint.activate([
containerView.leadingAnchor.constraint(equalTo: view.leadingAnchor),
containerView.trailingAnchor.constraint(equalTo: view.trailingAnchor),
containerView.bottomAnchor.constraint(equalTo: view.bottomAnchor),
containerView.heightAnchor.constraint(equalTo: view.heightAnchor, multiplier: 0.5),
toolbar.topAnchor.constraint(equalTo: containerView.topAnchor),
toolbar.leadingAnchor.constraint(equalTo: containerView.leadingAnchor),
toolbar.trailingAnchor.constraint(equalTo: containerView.trailingAnchor),
toolbar.heightAnchor.constraint(equalToConstant: 50.0),
weightPicker.topAnchor.constraint(equalTo: toolbar.bottomAnchor),
weightPicker.leadingAnchor.constraint(equalTo: containerView.leadingAnchor),
weightPicker.trailingAnchor.constraint(equalTo: containerView.trailingAnchor),
weightPicker.bottomAnchor.constraint(equalTo: containerView.bottomAnchor)
])
}