Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Segue Out of Navigation Controller

Tags:

ios

swift

segue

I am trying to leave the initial view controller, and go into the blank view controller. That is fine, but that would make the blank view controller also part of the navigation controller, which is not what I want. I want to segue out of the view controller.

In the view controller I try to segue out of, it pops it self, and when I try the method in the view will appear of the target view controller, self.navigationController?.topViewController returns itself, but self.navigationController?.popViewControllerAnimated(animated) doesn't work

enter image description here

like image 645
Saketram Durbha Avatar asked Dec 24 '15 23:12

Saketram Durbha


People also ask

How do I create a segue between view controllers?

To create a segue between view controllers in the same storyboard file, Control-click an appropriate element in the first view controller and drag to the target view controller. The starting point of a segue must be a view or object with a defined action, such as a control, bar button item, or gesture recognizer.

How to unwind segue?

In your storyboard, create an unwind segue by right-clicking a triggering object and dragging to the Exit control at the top of your view controller's scene.

What is the point of a navigation controller?

Anatomy of a Navigation Interface A navigation controller's primary job is to manage the presentation of your content view controllers, and it is also responsible for presenting some custom views of its own. Specifically, it presents a navigation bar, which contains a back button and some buttons you can customize.

What is a segue Swift?

Swift version: 5.6. Segues are visual connectors between view controllers in your storyboards, shown as lines between the two controllers. They allow you to present one view controller from another, optionally using adaptive presentation so iPads behave one way while iPhones behave another.


2 Answers

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" enter image description here

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.

like image 134
Rashwan L Avatar answered Sep 29 '22 23:09

Rashwan L


I believe this can be done without having to write any code at all. To Segue out of a Navigation Controller follow these steps:

  1. Create a Segue from the View Controller that has a Navigation Controller as it's root view controller to the View Controller that you would like to go to next (without being inside the Navigation Controller)
  2. Click on the new Seque
  3. Open the Attributes Inspector
  4. Choose 'Kind' -> 'Present Modally'
  5. Choose 'Present' -> 'Over Current Context'

enter image description here

like image 38
Pete Varley Avatar answered Sep 29 '22 21:09

Pete Varley