Segue Out of Navigation Controller
If you have a navigationController
do
self.navigationController?.popViewControllerAnimated(false)
Otherwise do
self.dismissViewControllerAnimated(false, completion: nil)
Update
Go to your Storyboard, select the ViewController you want to navigate to and add a storyboard id. Make sure the click "Use Storyboard ID"
Go to the class you want to navigate from and add the following code
let storyboard = UIStoryboard(name: "Main", bundle: nil)
// vc is the Storyboard ID that you added
// as! ... Add your ViewController class name that you want to navigate to
let controller = storyboard.instantiateViewControllerWithIdentifier("vc") as! ViewController
self.presentViewController(controller, animated: true, completion: { () -> Void in
})
Add this code in the action that you use when you want to navigate.
Swift - Permanent View Controller Segue (outside of Navigation Controller)
You can either change rootViewController of your main window:
self.window.rootViewController = vc
or you can just simply set vc's modalPresentationStyle to fullscreen (this doesn't have a "go back" option by itself, only you can dismiss it calling dismiss)
let vc = UIViewController()
vc.modalPresentationStyle = .fullScreen
or from storyboard change it's presentation style to fullscreen.
Get out of Navigation Controller with Segue
It sounds like you presented the NavigationController with a modal segue.
If this is the case you can call dismiss when you are ready to go back to starting view controller.
dismiss(animated: true, completion: nil)
Swift Dismiss navigation controller after prepare for segue
If I understand your code correctly, you're adding another segue back to AddPhotoVC. So if while your app was running and you clicked the "Debug View Hierarchy" (down by the debugger controls in Xcode) you'd see that you now have another AddPhotoVC on top of the original one.
Instead of performing a segue from SelectPhotoVC to AddPhotoVC you may want to consider performing an Unwind Segue instead. This way you could pass the values you want and all the previous VCs would be dismissed.
Segue and navigation controller interaction
From your screen shot it seems that the issue might be you have the tabBarController inside a navigationController.
You should use the TabBarController as the root view controller (arrow should point to it) and it shouldn't be embedded inside the stack of a navigation controller specifically it shouldn't be a navigationController's rootVC.
Read here Apple -NavigationController rootViewController
rootViewController The view controller that resides at the bottom of
the navigation stack. This object cannot be an instance of the
UITabBarController class.
Your below comments said that you rearranged everything by setting the tabBarVC as root to the application (arrow pointing to it) but it still didn't work. It's hard to identify the issue.
You should use print statements to find out which nav controller is doing the pushing, it might help you arrow down the problem.
override func viewDidLoad() {
super.viewDidLoad()
// is this the very navController that has the tabBar as root or is this the one of the other ones?
print("viewDidLoad: \(navigationController?.viewControllers.description as Any)") // this should print everything currently on the stack
}
How to get rid of navigation controller and segue to a controller before it?
By dismissing the root view controller of your key window, you dismiss all view controllers and go back to youe login vc assuming it's the initial view controller.
UIApplication.sharedApplication().keyWindow?.rootViewController?.dismissViewControllerAnimated(true, completion: nil)
How to segue to the root of navigation controller in Swift?
If this is a separate flow to the rest of your app, you could create a new navigation controller for this flow.
Create a new navigationController with screen 1 as its rootViewController
. Then set the navigationController as the initialViewController of that storyboard. Now this collection of screens will act as a self contained flow inside your main app. Doing this, you can simply call self.navigationController?.popToRootViewController(animated: true)
inside the button on screen 4, to return to the first screen.
Also just FYI,
- The reason the red segue has a back button to 4, is because a storyboard reference creates a new instance of the viewController. You are not returning to number 1 here, you are adding a new copy of screen 1 into the navigation stack.
- I don't know if its just the drawing or not, but having 1 screen per storyboard is a waste. Storyboards were designed to have many screens inside each
unwind segue Xcode 10 swift with navigation controller not working
From your comments, I'm guessing that the problem is that there is no FavouritesTableViewController in the view controller hierarchy at the time that the Save button is pressed. You cannot unwind to something that isn't already present: "unwind" means go back to something that was instantiated earlier and remains present, above the current view controller in the view controller hierarchy.
For example, let's say we have
UINavigationController -> (root view controller) -> FavouritesTableViewController
-> (push) ExplorationResultViewController
Now we can unwind from the Exploration controller to the Favourites controller, because the Favorites controller is already in the hierarchy before the Exploration controller. It sounds like maybe you are not appreciating that fact.
Related Topics
How to Make a Bullet List with Swift
Swift Get Specific Value from Firebase Database
iOS - Send Image to Instagram - Documentinteraction
Openurl in Appdelegate Conversion Error Nsstring -> String (Swift & iOS8)
How to Send Msmessage in Messages Extension
Manage a Uipickerview from an External Class - Using Swift
How to Run My Performance Tests More Than Ten Times
How to Add .Plist File to All Targets in Xcode
How to Apply Multiple Masks to Uiview
(Swift Spritekit) Rotate Sprite in the Direction of Touch
Today Extension with Uicollectionview Different Behaviour Compared to Single View Application
How to Automatically Create an Initializer for a Swift Class
How to Use Nsuserdefaults to Store an Array of Custom Classes in Swift
Xcode 6 & Swift: Black Bars Appear Above and Below the Viewcontroller on iOS 7 iPhone 5 Device