我在应用程序委托中创建了RootViewController,如下所示:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];

    StartViewController *viewController = [[StartViewController alloc] init];
    StartNavigationController *navigationController=[[StartNavigationController alloc] initWithRootViewController:viewController];
    self.window.rootViewController = navigationController;
    [self.window makeKeyAndVisible];
}


当我单击注销按钮时,我想将用户发送回rootview控制器:

- (IBAction) logoutButtonPressed:(UIButton *)sender
{
    [Users logOut];

    [self.navigationController popToRootViewControllerAnimated:YES];
}


当我在iPhone 4s上运行它(在到达iPhone 6时对其进行测试)时,此方法工作正常,但是如果我让用户登录超过一天并单击注销按钮,则屏幕将滑动为黑色。

为什么我的根视图控制器在大约24小时后没有调用startviewcontroller?

最佳答案

我正在尝试给出一个答案,我想它应该对您有用。首先,您检查StartViewController是否存在于navigationController堆栈中。按照您的堆栈,StartViewController应该是您在navigationController中添加的第一个控制器。

StartViewController *loginController=[self.navigationController.viewControllers objectAtIndex:0];


if(loginController){
   [self.navigationController popToViewController:loginController animated:YES];
}else{
    NSMutableArray *controllers=[[NSMutableArray alloc] init];

   loginController=[[StartViewController alloc] initWithNibName:@"StartViewController" bundle:nil];
    [controllers addObject:loginController];
    [controllers addObjectsFromArray:self.navigationController.viewControllers];
    self.navigationController.viewControllers=[[NSArray alloc] initWithArray:controllers];
    [self.navigationController popToRootViewControllerAnimated:YES];
}


我认为应该可以。

干杯。

10-07 20:28