返回IOS中的根视图 [英] Return to root view in IOS

查看:129
本文介绍了返回IOS中的根视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对某些人来说这听起来像是一个愚蠢的问题。我一直在搜索,发现很少,主要是因为我找不到合适的搜索字词。

To some this may sound like a daft question. I've searched around, and found little, mostly because I cannot find the appropriate search terms.

这里我想做的是:

应用程序从视图A开始。

The application begins at view A.

查看A启动视图B,视图B启动视图C.

View A launches view B, and view B launches view C.

对于视图C来说,它们是一种直接返回A而不会自行消除并因此暴露B的方式。例如主菜单按钮。

Is their a way for view C to return directly back to A without dismissing itself and thus exposing B. For example a main menu button.

推荐答案

我发现了解决问题的方法。它有点脏,(我可能会因此而被火焰击落)但在测试中效果很好并且很快实现。我是这样做的。

I have discovered a solution to my problem. Its a bit dirty, (and I''ll probably get shot down in flames for it) but works very well under tests and is very quick to implement. Here's how I did it.

在我的应用程序中,我有一个名为 GlobalVars 的Singleton类(我使用它来存储各种全局设置)。该类包含一个名为 home_pressed 的布尔值和相关的访问器(通过合成)。如果您愿意,也可以将此值存储在应用程序委托中。

In my app I have a Singleton class called GlobalVars (I use this for storing various global settings). This class holds a boolean called home_pressed and associated accessors (via synthesise). You could also store this value in the application delegate if you wish.

在每个带有主菜单按钮的视图控制器中,我将按钮连接到 homePressed IBAction方法如下。首先将全局 homePressed 布尔设置为 YES ,然后以常规方式关闭视图控制器,但使用 NO 动画。

In every view controller with a main menu button, I wire the button to the homePressed IBAction method as follows. First setting the global homePressed boolean to YES, then dismissing the view controller in the usual way, but with NO animation.

-(IBAction) homePressed: (id) sender
{
   [GlobalVars _instance].homePressed = YES;
   [self dismissModalViewControllerAnimated: NO];
}//end homePressed

在除主菜单之外的每个视图控制器中,我实现了 viewDidAppear 方法(在视图重新出现时调用),如下所示。

In every view controller except the main menu I implement the viewDidAppear method (which gets called when a view re-appears) as follows.

-(void) viewDidAppear: (Bool) animated
{
   if ([GlobalVars _instance].homePressed == YES)
   {
      [self dismissModalViewController: NO];
   }
   else
   {
      //put normal view did appear code here/
   }

}//end viewDidAppead

mainMenu 视图控制器中, root 应用程序,我在其视图中将全局 homePressed 布尔值设置为确实显示如下方法

In the mainMenu view controller which is the root of the app, I set the global homePressed boolean to NO in its view did appear method as follows

-(void) viewDidAppear: (Bool) animated
{
   if ([GlobalVars _instance].homePressed == YES)
   {
      [GlobalVars _instance].homePressed == NO;
   }
   else
   {
      //put normal view did appear code here/
   }

}//end viewDidAppear

在这里,这使我能够从链中的任何视图返回到我的应用程序的根主菜单。

There, this enables me to go back to the root main menu of my app from any view further down the chain.

我希望避免使用这种方法,但它比重新实现我的应用程序更好,如果我想使用UINavigationController解决方案,我必须这样做。

I was hoping to avoid this method, but its better than re-implementing my app which is what I'd have to do if I wanted use the UINavigationController solution.

简单,我花了10分钟在我的9视图应用程序中编码。 :)

Simple, took me 10 minutes to code in my 9 view app. :)

我有一个最后的问题,我的解决方案可以和HIG一起使用吗?

One final question I do have, would my solution be OK with the HIG?

这篇关于返回IOS中的根视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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