Newbie
Newbie

Reputation: 167

Swift: miss tab bar after click Back button

I'm new to swift and IOS development. I'm working on a tabbed application(3 views). For example, FirstView, SecondView and ThirdView. FirstView has a button that opens a addNewSession view and the addNewSession view has a Back button that back to the FirstView. The problem is Tab bar disappears after back from the addNewSession view

FirstView to addNewSession view.

 @IBAction func toAddNew(sender: AnyObject) {
            let addNewSession = self.storyboard?.instantiateViewControllerWithIdentifier("addNewSession") as addNew
            self.presentViewController(addNewSession, animated: true, completion: nil)
        }

addNewSession view to FirstView

@IBAction func backToPrev(sender: AnyObject) {
        println("test1")
        let FirstView = self.storyboard?.instantiateViewControllerWithIdentifier("FirstView") as FirstViewController
        self.presentViewController(FirstView, animated: true, completion: nil)
    }

Upvotes: 1

Views: 899

Answers (1)

Mike Taverne
Mike Taverne

Reputation: 9352

The problem is your backToPrev method is instantiating a new FirstViewController, which is not the same instance you came from. You are not really going back to the original one, you are showing a new one. This is not what you want.

The proper way to do this is to embed the FirstViewController in a navigation controller, then push the addNew controller onto it. When you use a nav controller, you get the Back behavior for free.

Hopefully you are using a storyboard? Select your FirstViewController, go to the Editor menu and choose Embed in Navigation Controller.

In your toAddNew, instead of presentViewController use self.navigationController.pushViewController to push your addNew controller.

There's an even easier way to do this last step, using segues. You control drag in the storyboard from your button in FirstViewController to the addNew controller and create a Show segue. This will automatically show your addNew controller when the button is touched. With this approach, you will want to remove your toAddNew IBAction and the connection since it's redundant.

Upvotes: 1

Related Questions