Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to go back to previous view controller of different storyboard?

I have 2 storyboards say A and B. Storyboard A has multiple view controllers. After some user action I switch from storyboard A to storyboard B. How can I go back to previous storyboard A on clicking back button and go back to same view controller I initially came from.

I can't do as given below because I will not have the old navigation stack of A.

 let storyboard =  UIStoryboard(name: "A", bundle: nil)
    let vc = storyboard.instantiateInitialViewController() as UIViewController!
    presentViewController(vc, animated: true, completion: nil)
like image 522
2ank3th Avatar asked Dec 03 '22 15:12

2ank3th


2 Answers

The "Back" button should be linked to something like this:

self.dismissViewControllerAnimated(true, completion: nil)
like image 67
Wen W Avatar answered Dec 27 '22 23:12

Wen W


if your viewcontrollers are embedded in a navigationcontroller you can do the following:

to move from storyboard A to storyboard B

let storyboard =  UIStoryboard(name: "B", bundle: nil)
let vc = storyboard.instantiateInitialViewController() as UIViewController!
navigationController?.pushViewController(vc, animated: true)

to get back from storyboard B to storyboard A

navigationController?.popViewControllerAnimated(true)

(of course the the initial viewcontroller in storyboard B should not be a navigationcontroller but a regular viewcontroller)

like image 42
André Slotta Avatar answered Dec 27 '22 23:12

André Slotta