Search code examples
iosswiftipadkeyboard-shortcuts

Why #selector doesn't call handler method for iPad's keyboard shortcuts?


I have a class for constructing 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)
    }
}

Class for constructing KeyboardToolbar from KeyboardToolbarButton:

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

}

And delegate:

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

Here's how I use 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)")


    }
}

Here's how it works (this feature available only on iPad)

enter image description here

So, the problem is that @objc func buttonTapped(sender: UIBarButtonItem) never calls. So, #selector(self.buttonTapped(sender:) does not connect to handler. How to fix it?

Update:

According to the answer of Taras Chernyshenko, I added KeyboardToolbar as a member of 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)
    }

}

Solution

  • Problem is in you design. In KeyboardToolbar class in func setup(leftButtons:, rightButtons:) function you doing next:

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

    here you are setting action target to KeyboardToolbar class.

    Next in addButtons(for textField:, setLeftButtons leftButtons:, andRightButtons rightButtons:) of ViewController you setup your buttons like

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

    but after this function toolbar is dealocated, so actions can't reach their target.

    To simple fix, store toolbar into class property

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