xcode 显示新视图控制器的最佳实践 (iPhone)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2270835/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 19:07:44  来源:igfitidea点击:

best-practices for displaying new view controllers ( iPhone )

iphonexcodeviewcontroller

提问by WoodenKitty

I need to display a couple of view controllers (eg, login screen, registration screen etc). What's the best way to bring each screen up?

我需要显示几个视图控制器(例如,登录屏幕、注册屏幕等)。显示每个屏幕的最佳方式是什么?

Currently for each screen that I'd like to display, I call a different method in the app delegate like this: Code:

目前,对于我想要显示的每个屏幕,我在应用程序委托中调用不同的方法,如下所示:

- (void) registerScreen
{
 RegistrationViewController *reg = [[RegistrationViewController alloc] initWithNibName:@"RegistrationViewController" bundle:nil];
 [window addSubview:reg.view]; 
}
- (void) LoginScreen
{
 LoginViewController *log = [[LoginViewController alloc] initWithNibName:@"LoginViewController" bundle:nil];
 [window addSubview:log.view]; 
}

It works, but I cant imagine it being the best way.

它有效,但我无法想象这是最好的方法。

回答by blindjesse

I'd recommend reading the View Controller Programming Guide if you haven't: http://developer.apple.com/iphone/library/featuredarticles/ViewControllerPGforiPhoneOS/Introduction/Introduction.html

如果您还没有,我建议您阅读 View Controller Programming Guide:http: //developer.apple.com/iphone/library/featuredarticles/ViewControllerPGforiPhoneOS/Introduction/Introduction.html

It sounds like presenting a view controller modally may be your best bet - but you'll probably want to wrap it in an UINavigationController first.

听起来以模态方式呈现视图控制器可能是您最好的选择 - 但您可能希望首先将其包装在 UINavigationController 中。

eg

例如

UINavigationController *navController = [[[UINavigationController alloc] initWithRootViewController:theControllerYouWantToPresent] autorelease];
[self presentModalViewController:navController animated:YES];

回答by Ian Henry

I've often wondered if this is the best way myself, but when I'm not using IB's built-in stuff (like a NavigationController) I have a single method in the AppDelegate, switchToViewController:(UIViewController *)viewControllerthat I pass...well, it's pretty self-explanatory I guess. This way there's only one place where it's done, and I can easily define transitions in that method once the app nears completion.

我经常想知道这是否是我自己最好的方法,但是当我不使用 IB 的内置内容(如 NavigationController)时,我在 AppDelegate 中有一个方法switchToViewController:(UIViewController *)viewController,我通过了......好吧,它很自我- 我猜是解释性的。这样只有一个地方完成,一旦应用程序接近完成,我可以轻松地在该方法中定义转换。

Also, don't forget to remove the previous views in your methods, otherwise you're liable to run out of memory. Something like this:

另外,不要忘记删除方法中以前的视图,否则可能会耗尽内存。像这样的东西:

-(void) switchToViewController:(UIViewController *)c {
    if(c == currentController) return;

    [currentController.view removeFromSuperview];
    [window addSubview:c.view];
    [currentController release];
    currentController = [c retain];
}