0

My app has four tabs that displays different information.

In my second tab view controller i have one button lets name it as button1 in that button1 action i have navigated to SignInViewController screen and in my third tab view controller is loginViewController.

Here purpose of both SignInViewController and loginViewController screen is same i.e.,user can logged into the app in both the ViewController's.

Here what i want exactly is, if I am logged in SignInViewController then whenever I tap on third TabBarItem View Controller should directly navigated to next screen of loginViewController i.e.,to that next screen i have named it as AccountViewController. I have tried below code in tabbarcontroller class but its does not working.

Please help me on this. Thanks in Advance.

- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController
{

       if (tabBarController.selectedIndex == 2){

        {

          if (![[[NSUserDefaults standardUserDefaults]objectForKey:@"SigninStatus"] isEqualToString:@"SigninSuccess"]){

                UIStoryboard *story =  [UIStoryboard storyboardWithName:@"Main" bundle:nil];
                LoginViewController *logInVc = [story instantiateViewControllerWithIdentifier:@"LoginViewController"];
                [self.navigationController pushViewController:logInVc animated:YES];
            }
            else
            {

                UIStoryboard *story =  [UIStoryboard storyboardWithName:@"Main" bundle:nil];
                AccountViewController *accountVc = [story instantiateViewControllerWithIdentifier:@"AccountViewController"];
                [self.navigationController pushViewController:accountVc animated:YES];

            }
        }
    }  
}
Sajida
  • 31
  • 9
  • Is it going to else condition? Did you check that? – Dhanunjaya Sep 28 '16 at 05:44
  • yes it is going to else condition i kept break points also i have checked step by step coding wise there is no problem but in simulator its doesn't navigating.. – Sajida Sep 28 '16 at 05:47
  • You mean when you tap on 3rd item from Tabbar "If user is already logged in" AccountViewcontroller should get pushed and in your case it is not getting pushed right? – Sagar... Sep 28 '16 at 05:48
  • yes sagar i have facing this problem since two days but till now i did not find any solution. – Sajida Sep 28 '16 at 05:49
  • Update the answer, I think I got what you actually want to achieve. – Bista Sep 28 '16 at 06:26

3 Answers3

0

1.Is your loginViewController is embedded inside UINavigationController?

-If not than embed it inside UINavigationController

2.Create a Push Segue from loginViewController to AccountViewController e.g. segue identifier: segueLoginToAccount

3.Now when user click on 3rd tab bar item, create a check in viewDidLoad on loginViewController, to see whether user has already logged in or not:

-(void)viewDidLoad {
    [super viewDidLoad];

    if ([[[NSUserDefaults standardUserDefaults]objectForKey:@"SigninStatus"] isEqualToString:@"SigninSuccess"]) {
        //already logged in, push to Account View
        [self performSegueWithIdentifier:@"segueLoginToAccount" sender:nil];
    }//else user will stay on Login View
}

No need to use tabBarController didSelectViewController: method.

Bista
  • 7,869
  • 3
  • 27
  • 55
0

First thing - If purpose of both viewcontroller is same then why different view controllers for both buttons ? You should use same viewcontroller for both cases.

Second thing you can store some flag or status in NSUserDefaults that contains information about user is loggedin or not.

Then just put if - else condition on button click that if user is logged in then instantiate next view controller instead of loginVC or if user is not logged in then show loginVC that's it.

Update :

As you asked in comment that you want this on tabbar's click then you should subclass - UITabBarController and set it to your UITabBarController in iterface builder and in that class you need to implement delegate method - didSelectItem and in this method check from userdefaults that user is logged in or not and set view controller accordingly!

Refer this SO post for more details!

Community
  • 1
  • 1
Ketan Parmar
  • 27,092
  • 9
  • 50
  • 75
  • Thanks for ur response.its not about button now.I want this scenario when TabBarItem is clicked – Sajida Sep 28 '16 at 06:05
0

You need to put 3rd view controller inside NavigationController. This will enable navigation stack for the controller to push on.

UIViewController* loginviewcontroller = [UIViewController alloc] init];

UINavigationController * navController = [[UINavigationController alloc] initWithRootViewController: loginviewcontroller];

Assign this navController in Tabbar.

e.g:

NSMutableArray* viewControllersList = [[NSMutableArray alloc] init];

[viewControllersList addObject: navController];

[tabBarController setTabBarControllers: viewControllersList];

Via storyboard you can embed Navigation controller like this

enter image description here

Hope this will help.

Sagar...
  • 1,062
  • 3
  • 15
  • 35