开发者

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

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 te开发者_JAVA百科chniques 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 .


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜