gpt4 book ai didi

ios - 为什么#selector 不调用 iPad 键盘快捷键的处理程序方法?

转载 作者:行者123 更新时间:2023-11-28 14:05:22 24 4
gpt4 key购买 nike

我有一个用于构建 UIBarButtonItems 的类:

enum KeyboardToolbarButton: Int {

case done = 0
case cancel
case back, backDisabled
case forward, forwardDisabled

func createButton(target: Any?, action: Selector?) -> UIBarButtonItem {
var button: UIBarButtonItem!

switch self {
case .back:
button = UIBarButtonItem(title: "<=", style: .plain, target: target, action: action)
case .backDisabled:
button = UIBarButtonItem(title: "<=", style: .plain, target: target, action: action)
button.isEnabled = false
case .forward:
button = UIBarButtonItem(title: "=>", style: .plain, target: target, action: action)
case .forwardDisabled:
button = UIBarButtonItem(title: "=>", style: .plain, target: target, action: action)
button.isEnabled = false
case .done:
button = UIBarButtonItem(title: "DONE", style: .plain, target: target, action: action)
case .cancel:
button = UIBarButtonItem(title: "CANCEL", style: .plain, target: target, action: action)
}
button.tag = rawValue
return button
}

static func detectType(barButton: UIBarButtonItem) -> KeyboardToolbarButton? {
return KeyboardToolbarButton(rawValue: barButton.tag)
}
}

从 KeyboardToolbarButton 构造 KeyboardToolbar 的类:

class KeyboardToolbar {

weak var toolBarDelegate: KeyboardToolbarDelegate?
var textField: UITextField!

init(textField: UITextField) {

self.textField = textField
self.textField.autocorrectionType = .no
self.textField.inputAssistantItem.leadingBarButtonGroups = []
self.textField.inputAssistantItem.trailingBarButtonGroups = []
}

func setup(leftButtons: [KeyboardToolbarButton], rightButtons: [KeyboardToolbarButton]) {

let leftBarButtons = leftButtons.map { (item) -> UIBarButtonItem in
return item.createButton(target: self, action: #selector(self.buttonTapped(sender:)))
}

let rightBarButtons = rightButtons.map { (item) -> UIBarButtonItem in
return item.createButton(target: self, action: #selector(self.buttonTapped(sender:)))
}

let groupLeading: UIBarButtonItemGroup = UIBarButtonItemGroup.init(barButtonItems: leftBarButtons, representativeItem: nil)
let groupTrailing: UIBarButtonItemGroup = UIBarButtonItemGroup.init(barButtonItems: rightBarButtons, representativeItem: nil)

textField.inputAssistantItem.leadingBarButtonGroups.append(groupLeading)
textField.inputAssistantItem.trailingBarButtonGroups.append(groupTrailing)
}


@objc func buttonTapped(sender: UIBarButtonItem) {
if let type = KeyboardToolbarButton.detectType(barButton: sender) {
print(type)
toolBarDelegate?.keyboardToolbar(button: sender, type: type, tappedIn: self)
}
}

}

和委托(delegate):

protocol KeyboardToolbarDelegate: class {
func keyboardToolbar(button: UIBarButtonItem, type: KeyboardToolbarButton, tappedIn toolbar: KeyboardToolbar)
}

这是我使用 KeyboardToolbar 的方式:

class ViewController: UIViewController {

@IBOutlet weak var textField: UITextField!

override func viewDidLoad() {
super.viewDidLoad()


addButtons(for: textField, setLeftButtons: [.back, .forward], andRightButtons: [.done])
}


private func addButtons(for textField: UITextField, setLeftButtons leftButtons: [KeyboardToolbarButton] = [], andRightButtons rightButtons: [KeyboardToolbarButton] = []) {
let toolbar = KeyboardToolbar(textField: textField)
toolbar.toolBarDelegate = self
toolbar.setup(leftButtons: leftButtons, rightButtons: rightButtons)
}

}

extension ViewController: KeyboardToolbarDelegate {
func keyboardToolbar(button: UIBarButtonItem, type: KeyboardToolbarButton, tappedIn toolbar: KeyboardToolbar) {

print("Tapped button type: \(type)")


}
}

工作原理如下(此功能仅适用于 iPad)

enter image description here

所以,问题是@objc func buttonTapped(sender: UIBarButtonItem) 永远不会调用。所以,#selector(self.buttonTapped(sender:) 没有连接到处理程序。如何解决?

更新:

根据Taras Chernyshenko的回答,我添加了KeyboardToolbar作为ViewController的成员:

class ViewController: UIViewController {

@IBOutlet weak var textField: UITextField!
var toolbar: KeyboardToolbar!

override func viewDidLoad() {
super.viewDidLoad()

addButtons(for: textField, setLeftButtons: [.back, .forward], andRightButtons: [.done])
}


private func addButtons(for textField: UITextField, setLeftButtons leftButtons: [KeyboardToolbarButton] = [], andRightButtons rightButtons: [KeyboardToolbarButton] = []) {
toolbar = KeyboardToolbar(textField: textField)
toolbar.toolBarDelegate = self
toolbar.setup(leftButtons: leftButtons, rightButtons: rightButtons)
}

}

最佳答案

问题出在你的设计上。在 KeyboardToolbar 类的 func setup(leftButtons:, rightButtons:) 函数中,您接下来要执行的操作:

let leftBarButtons = leftButtons.map { (item) -> UIBarButtonItem in
return item.createButton(target: self, action: #selector(self.buttonTapped(sender:)))
}

在这里,您将 action target 设置为 KeyboardToolbar 类。

接下来在 ViewControlleraddButtons(for textField:, setLeftButtons leftButtons:, andRightButtons rightButtons:) 中设置你的按钮

let toolbar = KeyboardToolbar(textField: textField)
toolbar.toolBarDelegate = self
toolbar.setup(leftButtons: leftButtons, rightButtons: rightButtons)

但是在这个函数 toolbar 被释放之后,所以 Action 无法到达他们的目标。

为了简单修复,将 toolbar 存储到类属性中

class ViewController: UIViewController {

@IBOutlet weak var textField: UITextField!

var toolbar = KeyboardToolbar?
override func viewDidLoad() {
super.viewDidLoad()


addButtons(for: textField, setLeftButtons: [.back, .forward], andRightButtons: [.done])
}


private func addButtons(for textField: UITextField, setLeftButtons leftButtons: [KeyboardToolbarButton] = [], andRightButtons rightButtons: [KeyboardToolbarButton] = []) {
let toolbar = KeyboardToolbar(textField: textField)
toolbar.toolBarDelegate = self
toolbar.setup(leftButtons: leftButtons, rightButtons: rightButtons)
self.toolbar = toolbar
}

}

关于ios - 为什么#selector 不调用 iPad 键盘快捷键的处理程序方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53048417/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com