0

I have the following view controllers stack.

First, my app will show an app tour page. (Say TourViewController - super class is UIViewController). Added this controller in AppDelegate as rootviewcontroller.

self.window.rootViewController = tourViewController;

Then from the tour page, if the user taps on "Signin" button, I'm presenting the second view controller (Say LoginViewController - super class is UIViewController).

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

After a successful login, I need to resign the second view controller (LoginViewController) and want to show a tab bar based view for further needs.

I tried this code inside the login success method.

[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;

    }];

Problems:

  • When I'm in the LoginViewController, I have two view controllers in my stack. So even I resign the LoginViewController, the another one (TourViewController) remains in the screen.

  • If I tried the above code, tab bat controller was successfully added as root view controller. But, when the LoginViewController resigns, the background was filled by TourViewController

What I need is, When I resign the LoginViewController, the background view should be tab bar controller instead of TourViewController.

Help needed!!

Confused
  • 3,846
  • 7
  • 45
  • 72
  • simple solution is that, assign your `loginviewcontroller` as root view instead presenting it, and then when you login successful assign your `tabbarcontroller` as rootview. – Dipen Panchasara Sep 09 '15 at 06:43

2 Answers2

0

Do one thing,

Create UINavigationController in AppDelegate.h so you can access it anywhere.

Logic

Whenever you need to change navigation controller you must have to put your Controller to Navigation stack.

So first of all you have to create ViewController/ Tabbarcontroller object and assign it to navigationController and then show the 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];
Chetan Prajapati
  • 2,249
  • 19
  • 24
0

u can change the root view controller in AppDelegate not in the success method of the loginNavigationController better u can do this way

in 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

in AppDelegate.m

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

and in 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;

}];

NOTE: above is not tested just try it once

Edit 1

one way u can do it but with different animation, form this answer u can change to second window by doing some animation for example

in 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

in 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];
    }];
 }
Community
  • 1
  • 1
Shankar BS
  • 8,394
  • 6
  • 41
  • 53
  • Hi, thanks for the response. I tried this, its working fine but not fills but requirement. BTW, Should I always initiate the tab bar in AppDelegate? – Confused Sep 09 '15 at 07:02
  • yes after login successful there is no need of loginview controller and app delegate is the one to hold your tabviewcontroller and in feature it makes easy to access your tabview controller form app delegate – Shankar BS Sep 09 '15 at 07:08
  • Your code works fine with proper declaration of tab bar controller. But I want to show the tab bar controller as the background when my second controller (LoginViewController) resigns. Can you help in that? – Confused Sep 09 '15 at 08:23