James Ham
James Ham

Reputation: 119

Create a Logout Button

NB: First App;

I am currently trying to create a log out button for an app that I have created. Essentially, on load, the user is presented with LoginViewController.xib with 2 text fields and a button, given that the text fields meet the arguments, when the button is pushed, the following argument is executed:

if (success) {
        AppDelegate *appDelegate = [UIApplication sharedApplication].delegate;
        [appDelegate.window setRootViewController:appDelegate.tabBarController];
    }

This works fine, and the user is taken into the app with a tab controller that switches between 3 xib's (Home, Settings, Table).

In the settings tab, I have a button "LogOut" which when pressed, I would like the user to be returned to the "LoginViewController.xib" but I can't seem to find any way of doing this from tutorials on youtube or on the web.

Please see below for the Settings coding;

SettingsViewController.h:

#import <UIKit/UIKit.h>

@interface SettingsViewController : UIViewController

- (IBAction)LogOutClick:(id)sender;

@end

SettingsViewController.m:

#import "SettingsViewController.h"

@interface SettingsViewController ()

@end

@implementation SettingsViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)LogOutClick:(id)sender {
    [self dismissViewControllerAnimated:YES completion:nil];
}

@end

Upvotes: 0

Views: 961

Answers (1)

Chris Loonam
Chris Loonam

Reputation: 5745

Calling dismissViewControllerAnimated: is not going to help as your tab bar controller/settings view controller is not presented by any view controllers. The original login controller, as far as I can tell from the code you have supplied, no longer exists in memory and, as a result, you can not "return" to it.

You have two options: one is to present the login view controller on the tab bar controller, and the other is to change the window's root view controller to the login controller. For example

//Present over tab bar
- (IBAction)LogOutClick:(id)sender {
    LoginViewController *loginController = [[LoginViewController alloc] initWithNibName:@"LoginViewController" bundle:nil];
    [self presentViewController:loginController animated:YES completion:nil];
}

//Switch root view controller
- (IBAction)LogOutClick:(id)sender {
    LoginViewController *loginController = [[LoginViewController alloc] initWithNibName:@"LoginViewController" bundle:nil];
    [appDelegate.window setRootViewController:loginController];
}

In my opinion, the first method is the better way of doing things. After the login controller has been dismissed, you can then populate the tab bar controller's views with the proper data. However, as the login controller already has a method that switches the root view controller of the window, it may be easier for you to just switch the root view controller back to the login controller.

Upvotes: 1

Related Questions