Naveen Kumar
Naveen Kumar

Reputation: 177

How to move to root ViewController on logout button click in iOS?

I have one login page and once i logged in it will take me to a home page with mkmapview .On top left i have one menu button on click of that one left slideview will open .Here am using a library called SlideNavigationController for opening leftview. In that left view i have one logout button .When i click logout button i have to open my login page. now when i click on logout button it will slideout and go to home page not to login page.

please check my project structure in following image link

screen image

This is my current code

- (IBAction)logout:(id)sender 
{
    HomeController *vieww=[[HomeController alloc]init];
    loginController *vie=[[loginController alloc]init];
    [[SlideNavigationController sharedInstance] popToRootAndSwitchToViewController:vie
                                                             withSlideOutAnimation:self.slideOutAnimationEnabled
                                                                     andCompletion:nil];
}

In this it will show a screen like this instead of login screen device screenshot

Please help

Upvotes: 0

Views: 2467

Answers (4)

Anurag Bhakuni
Anurag Bhakuni

Reputation: 2439

hope this will help you

for many good reason write goToLogin method in your Appdelegate

 -(void)goToLogin
    {
        yourLoginViewController *objyourLoginViewController =[[yourLoginViewController alloc]initWithNibName:@"yourLoginViewController" bundle:nil];
        self.objNavigationController =[[UINavigationController alloc]initWithRootViewController:objyourLoginViewController];
        self.window.rootViewController = self.objNavigationController;
    }

- (IBAction)logout:(id)sender 
{

[(AppDelegate *)[UIApplication sharedApplication].delegate) goToLogin];
}

Upvotes: 0

Abhishek Sharma
Abhishek Sharma

Reputation: 3283

Store your LoggedIn detail in NSUserDefaults

And then Check in viewWillAppear method of HomeViewController

You have to clear that NSUserDefaults on Click of LogOut Button and need to navigate HomeViewController

For example

You are storing USER_ID in NSUserDefaults at time of LoggedIn like following

[[NSUserDefaults standardUserDefaults] setValue:@“Your Value” forKey:@"USER_ID"]; [[NSUserDefaults standardUserDefaults] synchronize];

Now in HomeViewController in viewWillAppear method put following check.

if([[NSUserDefaults standardUserDefaults] objectForKey:@"USER_ID"]){
    //User already logged in.
}else{
    // User is not logged in so need to show login page.
}

Clear NSUserDefaults in LogOut Button Action via following.

[[NSUserDefaults standardUserDefaults] removeObjectForKey:@"USER_ID"];
[[NSUserDefaults standardUserDefaults] synchronize];

Upvotes: 0

Anil Prasad
Anil Prasad

Reputation: 711

You can use this code to get all ViewControllers in a NSArray after that you can back where you want with ObjectAtIndex value .

 NSArray *viewsArray = [self.navigationController viewControllers];
 UIViewController *chosenView = [viewsArray objectAtIndex:1];//set index where you want to go back
 [self.navigationController popToViewController:chosenView animated:YES];

Upvotes: 0

Rajesh Loganathan
Rajesh Loganathan

Reputation: 11217

Try this to go to root view controller when you click logout action

- (IBAction)logout:(id)sender {
    [self.navigationController popToRootViewControllerAnimated:YES];
}

Upvotes: 1

Related Questions