ios 在 Swift 中显示和关闭模态视图控制器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28139547/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 04:30:10  来源:igfitidea点击:

Display and dismiss a modal view controller in Swift

iosswiftsegue

提问by Cesare

When a button is pressed I want to segue between two view controllers by using a Modal Transition style CoverVerticaland 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:

当按下按钮时,我想通过使用模态转换样式在两个视图控制器之间进行切换CoverVertical,然后关闭它。有很多关于如何在目标 C 中做到这一点的信息,但在 Swift 中找不到任何好的信息。到目前为止,我已经这样做了,但我认为这是不正确的:

 @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:

我正在使用的 Dismiss 也不起作用:

@IBAction func statusSaved(sender: UIBarButtonItem) {

        self.dismissViewControllerAnimated(false, completion: { () -> Void in
            let usersVC: UsersViewController = self.storyboard?.instantiateViewControllerWithIdentifier("UsersViewController") as UsersViewController
       })
    }

回答by Cesare

Swift 5:

斯威夫特 5:

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

dismiss(animated: true, completion: nil)


Swift 2.2:

斯威夫特 2.2:

self.presentViewController(true, completion: nil)

Hide/dismiss a view controller:

隐藏/关闭视图控制器:

self.dismissViewControllerAnimated(true, completion: nil)

回答by fred

To Dismiss View Controller in Swift 3.0

在 Swift 3.0 中关闭视图控制器

self.dismiss(animated: true, completion: {})

回答by Marius Fanu

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

您可以使用presentViewController:animated:completion:dismissViewControllerAnimated:completion:方法UIViewController在此处查看文档

回答by Aks

Its pretty easy :

它很容易:

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

使用swift 3.0关闭模态视图:使用如下所示的dismiss Api:

> @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

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

回答by craft

Dismiss view controller in Swift 4:

在 Swift 4 中关闭视图控制器

dismiss(animated: true, completion: nil)