添加和删除视图 - 最佳实践
我现在开发了一些应用程序,全部使用单视图控制器。在应用程序委托中的每个应用程序中,我在“didFinishLaunchingWithOptions”和[window addSubview:view_controller.view]中分配/初始化第一个视图控制器。
现在我的问题是删除和添加辅助视图的最佳实践是什么。
目前,我执行以下操作:
• 在屏幕上当前的视图控制器中,我引用应用程序委托并调用一个名为“showSecondViewController”的方法,传递“self”
• 然后在应用程序委托中的“showSecondViewController”方法中,我删除从超级视图传入的“self”和[window addSubview: new_view];新的视图控制器。
然后我会说其中的几个删除/显示方法来显示/隐藏所需的每个视图控制器。
这是最佳实践吗?我刚刚与另一位 iPhone 开发人员讨论如何使用导航控制器(隐藏顶部栏)和“动画:否”推送和弹出视图控制器,而不是使用这种方法?哪个最好?
我开始考虑这个最初的原因是在观看了斯坦福大学的讲座并看到了这张幻灯片之后: http://screencast .com/t/N2RkZWIzMzkt 这让我认为我所做的事情是不正确的。
请指教。
谢谢 詹姆斯
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 Controller (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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在我看来,您正在尝试手动完成
UINavigationController
可以为您做的事情,所以我会这样做。这张幻灯片告诉您的是,不要像大球一样将所有内容放入您的应用程序委托中泥浆。这意味着,创建一个对象来保存和管理您的所有视图,并且不依赖于其他任何内容。如果它需要访问其他对象来使用,请使用 setter 或在
-init
方法中传递它们,但没有它请转到应用程序委托(或其他一些中心点)并请求它。这就是所谓的依赖注入。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.