简体   繁体   中英

How to force tap on UIBarButtonItem using RxSwift?

This is what I do to setup UIBarButtonItem :

mainView.userBarButtonItem.rx.tap.bind { _ in
    self.router.presentUserController(from: self)
}.disposed(by: bag)

but now I need to do something to call that closure. How?

You can simulate a click by doing:

if let target = mainView.userBarButtonItem.target, let action = mainView.userBarButtonItem.action {
  _ = target.perform(action, with: mainView.userBarButtonItem)
}

Will work after having subscribed to the tap sequence.

However, I don't really see why you would need to do this. I hope this is not for unit-testing...

Try this, Will work fine

    mainView.userBarButtonItem.rx.tap.subscribe(onNext: { [unowned self] in
        self.router.presentUserController(from: self)
    }).disposed(by: disposeBag)

Thanks

您可以这样做:

UIApplication.shared.sendAction(barButtonItem.action!, to: barButtonItem.target, from: self, for: nil)

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