Michal Michal - 6 months ago 153
Swift Question

LogOut button creates back button

I created a button in my

UITableViewController
as a Left Bar Button Item to log the current user out and segue and to the login
ViewController
.

The problem is that tapping it creates a back button on the login
ViewController
and user can come back to the previous view.


Answer

Your flow of navigation controller of app is wrong i think. Login view controller should be rootview controller of navigation controller or it should be initial view controller. You have another viewcontroller something like Wybierz miasto !! It shouldn't like this.

You can call

 [self.navigationController popToRootViewControllerAnimated:YES]; 

from any view controller of navigation stack to go rootview controller. (i.e. logout).

If you must require flow like this then you can hide and show backbutton like,

  self.navigationItem.hidesBackButton = YES; //NO for show

If you want to hide navigation bar for particular view controller you can do like

 -(void)viewWillAppear:(BOOL)animated{

     [self.navigationController.navigationBar setHidden:YES];
  }

 -(void)viewDidDisappear:(BOOL)animated{

  [self.navigationController.navigationBar setHidden:NO];
 }

Update in swift as asked in comment

    self.navigationItem.hidesBackButton = true

and

   self.navigationController!.popToRootViewControllerAnimated(true)

and

  override func viewWillAppear(animated: Bool) {
    self.navigationController!.navigationBar.hidden = true
}

override func viewDidDisappear(animated: Bool) {
    self.navigationController!.navigationBar.hidden = false
}

Avoid mistakes in swift if any! Not much familiar with swift!

Hope this will help :)

Comments