Subscribe to UINavigationController back button with RxSwift / RxCocoa

你说的曾经没有我的故事 提交于 2019-12-11 04:26:54

问题


I have a simple test project with a UINavigationController as my rootViewController.

I push the first ViewController which has a + button as right bar button item, I subscribe to its taps to present a new ViewController (which is identical to the previous one). The push segue works as I expect.

UIKit manages the back button for me and I think it is UINavigationController that makes the magic behind the scene.

Now, what I need to do is subscribe to the back button, but I can't find how to do this. I have tried with

  1. navigationController.navigationBar.backItem?.backBarButtonItem?.rx.tap

  2. navigationController.navigationItem.backBarButtonItem?.rx.tap but they seem to be nil, so the optional chaining prevents subscription.

Thanks for your help, Luca-


回答1:


I think your best bet here is to use the operator on UINavigationController.

navigationController.rx.willShow will trigger whenever a view controller is about to be shown. You can do any last minute clean up when it fires.



来源:https://stackoverflow.com/questions/44609844/subscribe-to-uinavigationcontroller-back-button-with-rxswift-rxcocoa

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!