Sometimes you need to develop application which contains one toolbar on all screens. And buttons on this toolbar should change it behaviour depending on selected screen or whole toolbar design should be changed.
Imagine the following situation: you have to develop app which contains 2 screens. Each screen contains toolbar with logo and "Export PDF" button, buttons at the bottom which allows to switch screens, like in the following mockup:
If user press "Export PDF" button, we should get content of the current screen and export it in to pdf file. If user press "My app logo" label we have to open "googl.com" url. If user press "Close" button we have to close current screen.
The simplest solution is to add view with button and logo on each screen. But in this case you have to handle pressing on logo label in 2 places - in first screen's controller and into second screen controller. It is not good because this behaviour is the same for both screens and it have to leave in one place (but be used for both screens).
The solution may be the following: we should have one RootViewController which will contain header view (with export button and logo) and footer view with "First Screen" and "Second Screen" buttons.
Between header and footer views we will add Container view. This view will show content of the selected screen.
If we want to add unique behaviour for "Export PDF" button for each screen we have to do the following:
- Make RootViewController as singleton (in order to access the same instance of it in First and Second controllers)
- Implement both controller as delegates of the RootViewController (in order to listen tapping on "Export PDF" button)
FirstViewController and SecondViewController have to assign themselves as delegate in viewWillAppear method and remove themselves as delegate in viewWillDisappear method. When "Export PDF" buttons is pressed we inform our delegate about this event and it can perform his unique logic.
The example project you can find here: https://www.dropbox.com/s/kth1kmpgpeftxtv/ViewControllerAsSingleton.zip
No comments:
Post a Comment