23

When a button is pressed I want to segue between two view controllers by using a Modal Transition style CoverVertical and then dismiss it. There is allot of info out there for how to do it in objective C but can't find any good info in Swift. So far I've done this but I don't think it's correct:

 @IBAction func insertStatus(sender: UIButton) {

         var StatusVC: StatusViewController = StatusViewController()
    var modalStyle: UIModalTransitionStyle = UIModalTransitionStyle.CoverVertical
    StatusVC.modalTransitionStyle = modalStyle
    self.presentViewController(StatusVC, animated: true, completion: nil)

    }

The Dismiss I am using like so does not work either:

@IBAction func statusSaved(sender: UIBarButtonItem) {

        self.dismissViewControllerAnimated(false, completion: { () -> Void in
            let usersVC: UsersViewController = self.storyboard?.instantiateViewControllerWithIdentifier("UsersViewController") as UsersViewController
       })
    }
Myxtic
  • 5,509
  • 5
  • 43
  • 68
  • Have you tried to connect the two ViewControllers using Storyboard?? – Mustafa Ibrahim Jan 25 '15 at 17:56
  • No, I know how to do it that way, but that doesn't allow you to dismiss it does it? I want to do it programmatically. –  Jan 25 '15 at 17:58
  • Sorry, can you explain in details what are you trying to do? Thanks. – Mustafa Ibrahim Jan 25 '15 at 18:03
  • I am on VC1. When a button is pushed I want to present VC2 Modally (e.g. Present Modally). I then want to update something in VC2 (e.g. some text) then when finished press save and it will dismiss the VC2. Hope this helps... –  Jan 25 '15 at 18:11
  • This is the kind of thing I am trying to achieve, but with just to VC: http://stackoverflow.com/questions/14907518/modal-view-controllers-how-to-display-and-dismiss –  Jan 25 '15 at 18:12

5 Answers5

48

Swift 5:

present(UIViewController(), animated: true, completion: nil)

dismiss(animated: true, completion: nil)

Swift 2.2:

self.presentViewController(true, completion: nil)

Hide/dismiss a view controller:

self.dismissViewControllerAnimated(true, completion: nil)
Cesare
  • 8,326
  • 14
  • 64
  • 116
  • Thanks I am getting there now, I have edited my question, now the screen just goes black? –  Jan 25 '15 at 18:51
  • Modify the second line to this: `var StatusVC: UIViewController = StatusViewController()`. If it doesn't work, replace it with this instead: `var StatusVC: StatusViewController = UIViewController()`. – Cesare Jan 25 '15 at 18:59
  • First one still goes black and second one asked for a nibName? I don't know what mine is? –  Jan 25 '15 at 20:20
  • The first one is correct. Did you remember to name the `UIViewController` as StatusVC in the Storyboard (via Identity Inspector, Class)? – Cesare Jan 25 '15 at 20:30
  • name it StatusVC or StatusViewController? either way it still just goes black. Can't figure it out... –  Jan 25 '15 at 20:36
  • StatusVC. Sorry, but what is StatusViewController? You still haven't answered my last question. – Cesare Jan 25 '15 at 20:37
  • StatusViewController is the ViewController I am trying to present, edit some text on, then dismiss. Yes it is set to StatusViewController. StatusVC is just the name of the variable I was using. –  Jan 25 '15 at 20:41
  • So you should probably use `let StatusViewController = storyboard.instantiateViewControllerWithIdentifier("StatusViewController") as StatusViewController` but I can't get it to work either. Remember to give StatusViewController a Storyboard ID in the Identity Inspector. – Cesare Jan 25 '15 at 20:53
  • Yes that works for it to present, thank you! Now I want to dismiss the statusViewController with also a button. Is this done in statusViewController? with the same code but just using the `self.dismissViewControllerAnimated(true, completion: nil)` –  Jan 25 '15 at 21:03
  • If you want to dismiss it with a button you should a) connect the button with the code `@IBAction` via action, b) then post the code there. – Cesare Jan 25 '15 at 21:07
  • I have tried this: `@IBAction func statusSaved(sender: UIBarButtonItem) { self.dismissViewControllerAnimated(false, completion: { () -> Void in let usersVC: UsersViewController = self.storyboard?.instantiateViewControllerWithIdentifier("UsersViewController") as UsersViewController }) }` Which works but it does not dismiss a modally. It just shots straight back, I want it to slide back down. –  Jan 25 '15 at 21:09
  • It's because you haven't assigned the transition a `modalTransitionStyle`. – Cesare Jan 25 '15 at 21:11
  • `self.dismissViewControllerAnimated(false` should be `self.dismissViewControllerAnimated(true` because you actually want it to be animated. – Cesare Jan 25 '15 at 21:14
9

To Dismiss View Controller in Swift 3.0

self.dismiss(animated: true, completion: {})
fred
  • 387
  • 1
  • 9
  • 12
  • 2
    It would be a bit confusing that you used the word "Hide", because Hide, feels like the object is still out there but you are not seeing it. In fact after the view controller disappears from the screen, its object is destroyed and the memory it was using is reclaimed by the system. – SLN Feb 08 '17 at 15:24
2

You can use presentViewController:animated:completion: and dismissViewControllerAnimated:completion: methods from UIViewController. See docs here

Marius Fanu
  • 5,989
  • 1
  • 15
  • 19
1

Its pretty easy :

to dismiss a modal view with swift 3.0 : Use dismiss Api like below :

> @IBAction func dismissClick(_ sender: Any) {
>         dismiss(animated: true, completion: nil)
>         
>     }

For present :

> @IBAction func dismissClick(_ sender: Any) {
> present(UIViewController(), animated: true, completion: nil)
>         
>     }

For more details here you go :

https://developer.apple.com/documentation/uikit/uiviewcontroller#//apple_ref/doc/uid/TP40006926-CH3-SW96

Aks
  • 4,339
  • 1
  • 33
  • 32
1

Dismiss view controller in Swift 4:

dismiss(animated: true, completion: nil)
craft
  • 1,448
  • 1
  • 12
  • 26