Fayad Fayad - 2 months ago 12
Objective-C Question

Navigating another screen when tabbar selected when user already logged in another tab-iOS

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

}
}
}
}

Answer

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.

Comments