在后台更改根视图控制器

Changing root view controller in the background

我有以下视图控制器堆栈。

首先,我的应用程序将显示一个应用程序导览页面。 (比如 TourViewController - super class 是 UIViewController)。在 AppDelegate 中添加了此控制器作为 rootviewcontroller。

self.window.rootViewController = tourViewController;

然后在游览页面中,如果用户点击 "Signin" 按钮,我将展示第二个视图控制器(比如 LoginViewController - super class 是 UIViewController).

UINavigationController *loginNavigationController = [[UINavigationController alloc] initWithRootViewController:self.loginViewController];
[self presentViewController:loginNavigationController animated:YES completion:nil];

成功登录后,我需要退出第二个视图控制器 (LoginViewController) 并希望显示基于标签栏的视图以满足进一步的需要。

我在登录成功方法中尝试了这段代码。

[self dismissViewControllerAnimated:YES completion:^{

        TabBarViewController *tabController = [[TabBarViewController alloc] init];

        [self presentViewController:tabController animated:NO completion:nil];

        AppDelegate *applicationDelegate = [[UIApplication sharedApplication] delegate];
        applicationDelegate.window.rootViewController = tabController;

    }];

问题:

我需要的是,当我退出 LoginViewController 时,背景视图应该是标签栏控制器而不是 TourViewController

需要帮助!!

做一件事,

在 AppDelegate.h 中创建 UINavigationController,以便您可以在任何地方访问它。

逻辑

每当您需要更改导航控制器时,您必须将控制器放入导航堆栈。

所以首先你必须创建 ViewController/ Tabbarcontroller 对象并将其分配给 navigationController 然后显示 navigationController。

AppDelegate* myDelegate = (((AppDelegate*) [UIApplication sharedApplication].delegate));
InitialViewController *initialVC = [self.storyboard instantiateViewControllerWithIdentifier:@“InitialVC"];
myDelegate.navController = [[UINavigationController alloc] initWithRootViewController:initialVC];
myDelegate.window.rootViewController = myDelegate.navController;
[myDelegate.window makeKeyAndVisible];

你可以在 AppDelegate 中更改根视图控制器,而不是在 loginNavigationController 的成功方法中,你可以这样做更好

AppDelegate.h

 #import <UIKit/UIKit.h>
 #import "TabControllerViewController.h"

@interface AppDelegate : UIResponder <UIApplicationDelegate>

@property (strong, nonatomic) UIWindow *window;

- (void)showTabController;  //add this method call from on success method of log in completion 
@end

AppDelegate.m

- (void)showTabController;
 {
   TabControllerViewController *tabController =    [[TabControllerViewController alloc]  initWithNibName:@"TabControllerViewController" bundle:nil];
  self.window.rootViewController = tabController;
 [self.window makeKeyAndVisible];
}

并在 loginNavigationController.m

[self dismissViewControllerAnimated:YES completion:^{

    //TabBarViewController *tabController = [[TabBarViewController alloc] init];

  //  [self presentViewController:tabController animated:NO completion:nil]; //no nee to present 

    AppDelegate *applicationDelegate = [[UIApplication sharedApplication] delegate];
     [applicationDelegate showTabController]; //there is no need to create a tab bar in loginview controller, create it in root view controller 
    //applicationDelegate.window.rootViewController = tabController;

}];

注意:以上内容未经测试,请尝试一次

编辑 1

你可以用不同的动画来做, 形成这个 answer 你可以通过做一些动画来改变到第二个 window

AppDelegate.h

#import <UIKit/UIKit.h>
#import "TabViewController.h"
#import "LoginViewController.h"

@interface AppDelegate : UIResponder <UIApplicationDelegate>

@property (strong, nonatomic) UIWindow *window; //holds initial window, holds tour and login controller
@property (strong, nonatomic) UIWindow *tabWindow; //holds only tab controller 
//..other code below is my test 
@property (strong, nonatomic) TabViewController *tabViewController;
@property (strong, nonatomic) LoginViewController *loginController;
- (void)showTabController;
@end

AppDelegate.m

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

   // Override point for customization after application launch.
   _loginController = [[LoginViewController alloc]initWithNibName:@"LoginViewController" bundle:nil];
   _tabViewController = [[TabViewController alloc] initWithNibName:@"TabViewController" bundle:nil];

    self.window.rootViewController = _loginController; //for test for your case it contains tour view controller  
    [self.window makeKeyAndVisible];
    return YES;
 }

 - (void)showTabController;
 {
    [UIView transitionWithView:self.window duration:0.5 options:UIViewAnimationOptionTransitionFlipFromLeft animations:^{
    self.window.rootViewController = _tabViewController;
    } completion:^(BOOL finished) {
      // [_tabWindow makeKeyAndVisible];
    }];
 }