应用程序将进入后台 -> 返回根视图菜单

5
我正在开发一个应用程序,其中有大约8个视图并使用导航控制器进行导航。第一个视图是主菜单。
我希望当用户按下主页按钮(应用进入后台)时,每个视图都会弹回到主视图。
我知道AppDelegate方法中的applicationDidEnterBackground和applicationWillEnterForeground。
我也知道从导航控制器调用的popToRootViewControllerAnimated方法。
我尝试在applicationDidEnterBackground中使用popToRootViewControllerAnimated。像这样:
[self.window.rootViewController.navigationController popToRootViewControllerAnimated:YES];

但这并不起作用。

请问您能告诉我这项工作的最佳选项吗?


改成 ...ControllerAnimated:NO] 会起作用吗? - James Webster
@q0re,你的rootViewController是什么? - MobileDev
4个回答

10

我认为你可以像这样使用NSNotificationCenter

applicationDidEnterBackgroundapplicationWillEnterForeground方法内添加以下代码:

[[NSNotificationCenter defaultCenter] postNotificationName:@"popToRoot" object:nil];

在您的rootViewControllerviewDidLoad方法中(这个方法总是在应用程序启动时出现),添加以下内容:

并将此代码复制到您的rootViewControllerviewDidLoad方法中(该方法总是在应用程序启动时出现):

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(popToRootViewControllerAnimated) name:@"popToRoot" object:nil];

然后在您的rootViewController中创建一个方法:

- (void)popToRootViewControllerAnimated
{
    [self.navigationController popToRootViewControllerAnimated:YES];
}

每当应用程序第一次启动时,NSNotificationCenter 将为名称为 popToRoot 的通知进行初始化,并为此准备一个方法 popToRootViewControllerAnimated

当应用程序进入后台时,NSNotificationCenter 将向 rootViewController's popToRootViewControllerAnimated 方法传递消息 @"popToRoot",并且 viewcontroller 将弹出到 rootview


工作得很好,但有没有办法以“隐形”方式弹回到根视图控制器?我总是在重新启动后看到退回到主菜单。 - q0re
[self.navigationController popToRootViewControllerAnimated:YES]; 中加入 animated:NO - Vaibhav Saran

2
你尝试过这样吗:-
[self.navigationController popToRootViewControllerAnimated:YES];

请将您的navigationController名称替换为此处的navigationController。

编辑:

在AppDelegate.h文件中

@interface AppDelegate : UIResponder <UIApplicationDelegate>
{
    UINavigationController *navMain;
}

@property (nonatomic, retain) UINavigationController *navMain;

在AppDelegate.m文件中

@synthesize navMain;

-(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
    // Override point for customization after application launch.
    self.viewController = [[[ViewController alloc] initWithNibName:@"ViewController" bundle:nil] autorelease];
    self.navMain = [[UINavigationController alloc] initWithRootViewController:self.viewController];
    self.window.rootViewController = self.navMain;
    [self.window makeKeyAndVisible];
    return YES;
}

-(void)applicationDidEnterBackground:(UIApplication *)application
{
    NSLog(@"applicationDidEnterBackground");
    [self.navMain popToRootViewControllerAnimated:YES];
}

尝试编辑答案


我在 appDelegate 中创建了一个 navigationController,但它不起作用。而且 self 在 appDelegate 中没有导航控制器。 - q0re
请展示你在AppDelegate.m文件中的didFinishLaunchingWithOptions完整方法。 - D-eptdeveloper
在 didFinis.. 中,我只创建了一个闪屏视图而没有使用导航控制器。 - q0re
好的,我会向你展示我是如何做到这一点的,请看我的编辑后的答案,它可以正常工作。我正在更新我的答案。 - D-eptdeveloper
谢谢,但我使用Storyboard,而appDelegate的self没有viewController属性。 - q0re

0

第一点:你应该检查rootViewController是否为navigationController。因为self.window.rootViewController.navigationController经常为nil。 为什么? 因为navigationController的navigationController是“nil”。大多数情况下,我会将我的rootViewController设置为navigationController

第二点: 当你的应用程序即将退出时,你不应该做动画。你应该使用非动画方式。

popToRootViewControllerAnimated:NO

0
在您的AppDelegate类中为UINavigationController创建一个属性。在applicationDidEnterBackground:方法中,通过使用UINavigationController属性调用popToRootViewController方法。例如,如果您的属性名称为navigationController,则可以使用以下代码:
[self.navigationController popToRootViewControllerAnimated:YES];

是的,没错,但我必须在 didFinishLaunchingWithOptions 方法中初始化 navigationController 属性。而且我不能在没有视图控制器(在 AppDelegate 中)的情况下进行初始化。 - q0re

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接