Adding and Removing Views - Best Practice
I have developed a few applications now all using single view controllers. In each application in the application delegate I alloc/init the first view controller in "didFinishLaunchingWithOptions" and [window addSubview: view_controller.view].
Now my question is what is best practice for removing and adding secondary views.
At the moment I do the following:
• In my current View Controller on the screen I make a reference to the Application Delegate and call a method say called "showSecondViewController" passing "self"
• Then in the Application Delegate in the "showSecondViewController" method I remove the "self" passed in from the superview and [window addSubview: new_view]; the new view controller.I would then say several of these remove/show methods to show/hide each view controller needed.
Is this best practice? I was just talking to another iPhone developer about using instead of using this methodology using a Navigation Controlle开发者_StackOverflowr (hiding the top bar) and "animated:NO" pushing and pop'ing the view controllers? Which is best?
The reason also I started thinking about this initial was after watching the Standford University lectures and saw this slide: http://screencast.com/t/N2RkZWIzMzkt which is making me think that what I am doing is not correct.
Please advise.
Thanks James
Seems to me that you’re trying to do by hand what UINavigationController
can do for you, so I’d go with that.
What this slide is telling you is don’t put everything into your app delegate like a big ball of mud. That means, create an object that holds and manages all your views and doesn’t depend on anything else. If it needs access to other objects to work with pass them in using setters or in the -init
method, but don’t have it go to the app delegate (or some other central point) and ask for it. That’s called dependency injection.
精彩评论