在后台更改根视图控制器

时间:2015-09-09 06:34:41

标签: ios uitabbarcontroller dismiss rootview

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

首先,我的应用程序将显示应用程序浏览页面。 (说TourViewController - 超类是UIViewController)。在AppDelegate中添加了此控制器作为rootviewcontroller。

self.window.rootViewController = tourViewController;

然后从浏览页面,如果用户点击“登录”按钮,我将呈现第二个视图控制器(说LoginViewController - 超类是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时,我的堆栈中有两个视图控制器。所以即使我辞职LoginViewController,另一个(TourViewController)仍然在屏幕上。

  • 如果我尝试了上面的代码,则tab bat控制器已成功添加为根视图控制器。但是,当LoginViewController辞职时,背景由TourViewController填充

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

需要帮助!!

2 个答案:

答案 0 :(得分:0)

做一件事,

在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];

答案 1 :(得分:0)

你可以更改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形成你可以通过做一些动画来改变到第二个窗口,例如

in 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];
    }];
 }