Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to dismiss ViewController in Swift?

I am trying to dismiss a ViewController in swift by calling dismissViewController in an IBAction

  @IBAction func cancel(sender: AnyObject) {
    self.dismissViewControllerAnimated(false, completion: nil)
    println("cancel")
}

@IBAction func done(sender: AnyObject) {
    self.dismissViewControllerAnimated(false, completion: nil)
    println("done")
}

random image of a segue

I could see the println message in console output but ViewController never gets dismissed. What could be the problem?

like image 629
rshankar Avatar asked Oct 07 '22 02:10

rshankar


People also ask

How do I dismiss a presented ViewController?

When it comes time to dismiss a presented view controller, the preferred approach is to let the presenting view controller dismiss it. In other words, whenever possible, the same view controller that presented the view controller should also take responsibility for dismissing it.

How do you dismiss a storyboard?

Select the button that should make the UIViewController Disappear and drag it to the UIViewController you want to go to. In my case it shows **dismiss Controller* because of the name of my Class. Select it and you are done!

How to present/dismiss UIViewController’s view in Swift example?

How To Present / Dismiss UIViewController’s View In Swift Example. 1 There is a yellow background button with blue text in the main UIViewController view. 2 When you click the button, it will present the second UIViewController view. 3 The second UIView window has red background color, and there is a Dismiss This View button in it. More items...

How do I dismiss the wrong view controller?

For reference, be aware that you might be dismissing the wrong view controller. For example, if you have an alert box or modal showing on top of another modal. (You could have a Twitter post alert showing on top of your current modal alert, for example). In this case, you need to call dismiss twice, or use an unwind segue.

How do I dismiss the second UIView window?

The second UIView window has a red background color, and there is a Dismiss This View button in it. When you click the Dismiss This View button, it will dismiss the second UIView and return back to the main UIView.

What is UIViewController in Xcode?

When you develop an iOS app use swift in Xcode, the project template commonly creates a default UIViewController class defined in ViewController.swift file. This UIViewController class is used as a controller of the MVC pattern to respond to the app’s main window view’s event.


2 Answers

From you image it seems like you presented the ViewController using push

The dismissViewControllerAnimated is used to close ViewControllers that presented using modal

Swift 2

navigationController.popViewControllerAnimated(true)

Swift 4

navigationController?.popViewController(animated: true)

dismiss(animated: true, completion: nil)
like image 458
Zoon Nooz Avatar answered Oct 08 '22 15:10

Zoon Nooz


I have a solution for your problem. Please try this code to dismiss the view controller if you present the view using modal:

Swift 3:

self.dismiss(animated: true, completion: nil)

OR

If you present the view using "push" segue

self.navigationController?.popViewController(animated: true)
like image 188
satheesh Avatar answered Oct 08 '22 14:10

satheesh