简体   繁体   中英

How to add UIButton action in closure in custom UIToolbar class initialization?

I have a custom UIToolbar class with two UIBarButtonItem. I know I can create custom delegate action for UIBarButtonItem item. But how can I use closure as UIBarButtonItem action in custom UIToolbar class initialization?

class KeyboardToolBar: UIToolbar
{
    let done = UIButton.init()

    init() {
        super.init(frame: .zero)
        self.backgroundColor = UIColor.gray
        self.sizeToFit()
        let flexBarBtn = UIBarButtonItem.init(barButtonSystemItem: UIBarButtonSystemItem.flexibleSpace, target: nil, action: nil)
        done.frame = CGRect(x: 0, y: 0, width: 50, height: 44)
        done.setTitle("Done", for: .normal)
        done.setTitleColor(.black, for: .normal)
        let doneBarBtn:UIBarButtonItem! = UIBarButtonItem.init(customView: done)
        self.items = [flexBarBtn,doneBarBtn]
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

In ViewController

  super.viewDidLoad()

    txt.keyboardType = .numberPad
    txt.inputAccessoryView = KeyboardToolBar()
    // How can I use some thing like this
    // txt.inputAccessoryView = KeyboardToolBar(doneBtnAction: { 
    // print("done button pressed")
    // })
}

Here you go ...

class KeyboardToolBar: UIToolbar {
    let done = UIButton.init()
    var doneBtnAction:((Void) -> Void)?

    convenience init(_ doneBtnAction: @escaping (Void) -> Void) {
        self.init()
        self.doneBtnAction = doneBtnAction
    }

    private init() {
        super.init(frame: .zero)
        self.backgroundColor = UIColor.gray
        self.sizeToFit()
        let flexBarBtn = UIBarButtonItem.init(barButtonSystemItem: UIBarButtonSystemItem.flexibleSpace, target: nil, action: nil)
        done.frame = CGRect(x: 0, y: 0, width: 50, height: 44)
        done.setTitle("Done", for: .normal)
        done.setTitleColor(.black, for: .normal)
        done.addTarget(self, action: #selector(callbackDoneButton(_:)), for: .touchUpInside)
        let doneBarBtn:UIBarButtonItem! = UIBarButtonItem.init(customView: done)
        self.items = [flexBarBtn,doneBarBtn]
    }

    func callbackDoneButton(_ id:Any) -> Void {
        if self.doneBtnAction != nil {
            self.doneBtnAction!()
        }
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

In ViewController

override func viewDidLoad() {
    super.viewDidLoad()

    // Do any additional setup after loading the view, typically from a nib.
    txt.keyboardType = .numberPad
    txt.inputAccessoryView = KeyboardToolBar.init( {
        (Void) -> Void in
        print("done");
    })
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM