Bhavin Kansagara
Bhavin Kansagara

Reputation: 2916

How to poptoRootViewController from presented ViewController

I have login screen after login I m pushing to HomeViewController. On HomeViewController I have a button "Setting" on pressing which I am presenting "SettingViewController", Now on settingViewController I have button for Logout. Now when user press logout it should popToRootViewController.ie on LoginScreen but it is not being done. any suggestions. Here is my code.

on login

-(void)login_Success
{
HomeViewController *homeView = [[HomeViewController alloc]init];
[[self navigationController]pushViewController:homeView animated:YES];
}

on home presenting setting screen

- (IBAction)btn_setting_Click:(id)sender
{
SettingsViewController *settingsViewController = [[SettingsViewController alloc]init];
[self presentModalViewController:settingsViewController animated:YES];  
}

on setting screen loggout pressed

- (IBAction)btnLogout_Click:(id)sender
 {
[appDelegate closeSession];
[self dismissModalViewControllerAnimated:NO];
 [[[self parentViewController]navigationController] popToRootViewControllerAnimated:YES];
 }

This is not popping to Login screen on pressing logout. Any suggestions.

Upvotes: 0

Views: 1811

Answers (3)

βhargavḯ
βhargavḯ

Reputation: 9836

- (IBAction)btnLogout_Click:(id)sender
{
   [appDelegate closeSession];
   [self dismissModalViewControllerAnimated:NO];
   [[self.view.window.homeView navigationController] popToRootViewControllerAnimated:YES];
}

I am not sure but try this

EDIT

Using delegate try to implement this like this

SettingViewController.h

@protocol SettingsViewControllerDelegate
    -(void)logOut;
@end
...
@property(nonatomic, assign) id<SettingsViewControllerDelegate> delegate;

SettingViewController.m

@synthesize @delegate;

- (IBAction)btnLogout_Click:(id)sender
{
    [appDelegate closeSession];
    [self dismissModalViewControllerAnimated:NO];
    [self.delegate logOut];
}

HomeViewController.h

#import "SettingViewController.h"
@interface HomeViewController : UIViewController <SettingsViewControllerDelegate>

HomeViewController.m

- (IBAction)btn_setting_Click:(id)sender
{
    SettingsViewController *settingsViewController = [[SettingsViewController alloc]init];
    settingsViewController.delegate = self
    [self presentModalViewController:settingsViewController animated:YES];  
}

-(void)logout
{
    [[self navigationController] popToRootViewControllerAnimated:YES];
}

Upvotes: 0

Sunil Zalavadiya
Sunil Zalavadiya

Reputation: 1993

First define method in AppDelegate like following:

-(void)logoutNav
{
    [self.navigationController popToRootViewControllerAnimated:YES];
}

then try following code at SettingViewController

- (IBAction)btnLogout_Click:(id)sender
 {
    [self dismissModalViewControllerAnimated:NO];
    [((AppDelegate *)[[UIApplication sharedApplication] delegate]) logoutNav];
 }

Upvotes: 4

Niru Mukund Shah
Niru Mukund Shah

Reputation: 4733

You have presented your view controller in between. So in next view popping will not be available. Not even for rootViewController.

You need to dismiss your view, then pop to your previous view

Enjoy Programming!!

Upvotes: 0

Related Questions