在 iOS 中管理和关闭多个视图控制器 [英] Managing and dismissing Multiple View Controllers in iOS

查看:43
本文介绍了在 iOS 中管理和关闭多个视图控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 iPhone 的新手程序员.在开发我的第一个游戏/应用程序时,我为自己提出了一个问题(创造了一个问题).我对此进行了研究,并认为我已经看到了答案,但我不明白如何使它们适用于我的应用程序.

I am a novice programmer for the iPhone. In developing my first game/app, I have come up with a problem (created a problem) for myself. I have researched this and think I have seen answers but I don't understand how to make them work for my application.

我的游戏有几个视图控制器:欢迎、游戏、高分、首选项和关于.我没有我知道的导航控制器或根控制器.编辑 - 我现在有一个 rootview 控制器 - 我添加了 [self.window setRootViewController:viewController]; 到我的 appDelegate.m 文件

I have a game that has a few view controllers: Welcome, Play, High Scores, Preferences and About. I don't have a navigation controller or a root controller of which I am aware. EDIT - I now have a rootview controller - I added [self.window setRootViewController:viewController]; to my appDelegate.m file

从欢迎屏幕/视图控制器我可以转到播放视图控制器、关于视图控制器、高分视图控制器或首选项视图控制器.我想让我现在去的任何新控制器负责,以便我可以关闭欢迎视图控制器.事实上,每当我去到一个视图控制器时,我都希望能够从内存中消除任何其他视图控制器——尤其是我刚来的那个.我可以大多数视图控制器大多数视图控制器.

From the Welcome screen/viewcontroller I can go to the Play view controller, the about view controller, the high scores view controller or the prefs view controller. I would like to make whichever new controller I go to now be in charge, so that I can dismiss the Welcome view controller. In fact, any time I go to a view controller, I would like to be able to dismiss from memory any of the other view controllers - especially the one I just came from. I can go to most of the viewcontrollers from most of the viewcontrollers.

我使用这个去新的视图控制器:

I go to the new view controllers using this:

- (IBAction)playGameAction:(id)sender {
    FlipTestViewController *myViewController = [[FlipTestViewController alloc]

   initWithNibName:@"FlipTestViewController" bundle:nil];

    [UIView beginAnimations:@"flipview" context:nil];
    [UIView setAnimationDuration:1];
    [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
    [UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft 
                           forView:self.view cache:YES];

    [self.view addSubview:myViewController.view];

    [UIView commitAnimations];
}

一旦我安全地进入 FlipTestViewController(播放控制器),我该怎么做才能让 WeclomeViewController 离开?我如何告诉新控制器它现在负责而其他人可以离开?我什至问一个有意义的问题吗?回答时请用小词和具体例子!谢谢!

What do I do to let the WeclomeViewController go, once I am safely in the FlipTestViewController (the play controller)? And how do I tell the new controller that it is now in charge and the others can go away? Am I even asking a question that makes sense? Please use small words and concrete examples when responding! Thank you!

根据 rdelmar 的建议,我尝试添加一个调用以将 RootvVewController 更改为我的按钮代码 - 我用来调出新 viewController 的代码:

On rdelmar's advice, I tried adding a call to change the RootvVewController into my button code - the code I use to bring up the new viewController:

- (IBAction)playGameAction:(id)sender {
    FlipTestViewController *myViewController = [[FlipTestViewController alloc]
                                                      initWithNibName:@"FlipTestViewController"
                                                      bundle:nil];

    [UIApplication sharedApplication].delegate.window.rootViewController = myViewController;
    [UIView beginAnimations:@"flipview" context:nil];
    [UIView setAnimationDuration:1];
    [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
    [UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft 
                           forView:self.view cache:YES];

    [self.view addSubview:myViewController.view];

    [UIView commitAnimations];

}

果然,然后我看评论,FlipTestViewController(我的播放控制器)是新的RootViewController.不幸的是,我得到一个空白的白屏,没有任何类型的转换,并且日志消息告诉我我的 FlipTestViewController 中的 ViewDidDisappear 正在以某种方式被调用.可能是因为欢迎屏幕不知何故仍然存在.也许是内存管理问题?我应该把这个带到一个新问题吗?

Sure enough, then I look in the comments, the FlipTestViewController (my Play Controller) is the new RootViewController. Unfortunately, I get a blank white screen, no transition of any kind and log message telling me that my ViewDidDisappear in my FlipTestViewController is being called somehow. Probably because the Welcome Screen is still there somehow. Memory management problem perhaps? Ought I to take this to a new question?

我刚刚更改了 main.xib 文件的背景颜色,显然这就是显示的屏幕(空白屏幕).

I just changed the background color of the main.xib file and apparently that is the screen that is showing up (the blank white screen).

推荐答案

听起来最简单的方法就是重置应用程序窗口的 rootViewController 属性.从应用程序的任何地方,您都可以通过 [UIApplication sharedApplication].delegate.window.rootViewController 获取对根视图控制器的引用.因此,无论您使用何种操作方法切换到下一个视图控制器,您都可以分配初始化该控制器,然后将窗口的 rootViewController 属性设置为该控制器.如果您没有保留对控制器的其他引用,则在您重置属性时应该释放旧的控制器(这可能是您想要的,也可能不是您想要的——您可能希望拥有一些可以跟踪高分或位置的属性例如,玩家正在玩游戏).

It sounds like the easiest way to achieve what you want is to just reset the rootViewController property of the app's window. From anywhere in the app you can get a reference to the root view controller with [UIApplication sharedApplication].delegate.window.rootViewController. So, in whatever action method you're using to switch to the next view controller, you could alloc init that controller and then set the window's rootViewController property to that controller. If you keep no other reference to a controller, the old one should be deallocated when you reset the property (which may or may not be what you want -- you might want to have some properties that would keep track of the high score or where the player was in a game, for instance).

在应用程序委托中通常有这样的代码(当您从单个视图项目开始时):

In the app delegate there is usually code something like this (when you start with a single view project):

 WelcomeController *welcome = [[WelcomeController alloc] initWithNibName:@"ViewController" bundle:nil];
 self.window.rootViewController = welcome;

所以我会把类似的东西放在你的应用程序委托中,以及你想在启动时显示的任何视图控制器.然后在您的视图控制器代码(或您将按钮用于切换视图控制器的任何地方)中,您将拥有类似于(我在我的测试项目中使用分段控件)的内容:

So I would put something like that in your app delegate with whatever view controller you want to show at start up. Then in your view controller code (or wherever your putting your buttons to switch view controllers) you would have something similar to (I used a segmented control in my test project):

-(IBAction)selectController:(UISegmentedControl *)sender {
    if (sender.selectedSegmentIndex == 0) {
        PlayViewController *player = [[PlayViewController alloc] initWithNibName:@"PlayViewController" bundle:nil];
        [UIApplication sharedApplication].delegate.window.rootViewController = player;
    }else{
        HighScores *scorer = [[HighScores alloc] initWithNibName:@"HighScores" bundle:nil];
        [UIApplication sharedApplication].delegate.window.rootViewController = scorer;
    }
}

这篇关于在 iOS 中管理和关闭多个视图控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆