Switching views for iphone application - is this the right way?

独自空忆成欢 提交于 2019-12-25 08:36:48

问题


After looking for many hours to find a working view switching code, I finally found a great tutorial on YouTube. It was exactly what I needed as I needed to switch views when buttons are pressed.

I just wonder if the techniques used in that video are valid. The used code to switch screens is

viewsViewController *second = [[viewsViewController alloc] initWithNibName:nil bundle:nil];
[self presentModalViewController:second animated:YES];  

Where viewsViewController (or any class that is used there) is a class that's a subclass of UIViewController. This class is made by clicking File > New File > Add UIViewController subclass.

Is this method according to the Apple guidelines? Is this method memory friendly?

I sure hope the technique is valid. All other examples contained too little information so I couldn't make the example to work. And this is very stylish and short code which works .


回答1:


That's one way to do it. When you present a view controller modally, there's some expectation that the view controller will go away at some point and the user will return to the parent view controller.

Another option is to employ a UINavigationController, to which you can send -pushViewController:animated: messages.

Yet another way is to let a UITabBarController switch view controllers for you when the user hits the corresponding tab.

One more: you can set the window's rootViewController property, at which point the window will add the view controller's view as a subview of itself.

Your best bet is to read through the View Controller Programming Guide for iOS.




回答2:


This is one way of doing it. Be aware of some issues. Both views will be in memory, which is okay if they are a reasonable size. The modal view animates above the parent.

There are other models that you can use for simple navigation, if that's your main goal. I would suggest looking at UINavigationController and see if that might better meet your needs. It is probably the most common method to navigate views and provided a lot of the foundation of view management saving you that effort.



来源:https://stackoverflow.com/questions/7248194/switching-views-for-iphone-application-is-this-the-right-way

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