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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/37771001/
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 09:19:57  来源:igfitidea点击:

Dismiss and Present View Controller in Swift

iosswiftuiviewcontroller

提问by Ron Pelayo

Hi I'm trying to present a viewcontroller and dismiss my current modal view but this code is not working

嗨,我正在尝试呈现一个视图控制器并关闭我当前的模式视图,但此代码不起作用

self.dismissViewControllerAnimated(true, completion: {
    let vc = self.storyboard?.instantiateViewControllerWithIdentifier("OrderViewController")
    self.presentViewController(vc!, animated: true, completion: nil)
})

vice versa is not working too on completion block of presentviewcontroller

反之亦然在presentviewcontroller的完成块上不起作用

EDIT: replaced vc! to self

编辑:替换vc!对自己

回答by Sandeep Kumar

You have to get the viewController which presented self (current ViewController). If that view controller is rootViewController, then you can use the code below, if not then query it based on your view controller hierarchy.

您必须获得呈现自我的视图控制器(当前的视图控制器)。如果该视图控制器是 rootViewController,那么您可以使用下面的代码,如果不是,则根据您的视图控制器层次结构查询它。

if let vc3 = self.storyboard?.instantiateViewController(withIdentifier: "vc3") as? ViewController3 {
    let appDelegate = UIApplication.shared.delegate as! AppDelegate
    appDelegate.window?.rootViewController!.present(vc3, animated: true, completion: nil)
}

回答by DariusV

you can't do that because when the UIViewController A calls the UIViewController B and the first controller is dismissed then the two controllers are nil.

你不能这样做,因为当 UIViewController A 调用 UIViewController B 并且第一个控制器被解除时,两个控制器为零。

You need to have a UIViewController as a base, in this case MainViewController is the base. You need to use a protocol to call the navigation between controllers.

您需要有一个 UIViewController 作为基础,在这种情况下 MainViewController 是基础。您需要使用协议来调用控制器之间的导航。

you can do using protocol let say for example as bellow:-

您可以使用协议来做,例如如下:-

In to your viewController setting Protocol :

在您的 viewController 设置协议中:

    protocol FirstViewControllerProtocol {
    func dismissViewController()
}

class FirstViewController: UIViewController {
    var delegate:FirstViewControllerProtocol!

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


    @IBAction func goBack(sender: AnyObject) {
        self.dismissViewControllerAnimated(true) { 
            self.delegate!.dismissViewController()
        }
    }

Now in your main view controller

现在在您的主视图控制器中

class MainViewController: UIViewController, FirstViewControllerProtocol {

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}


@IBAction func goToFirstViewController(sender: AnyObject) {
    let viewController = self.storyboard?.instantiateViewControllerWithIdentifier(String(FirstViewController)) as! FirstViewController
    viewController.delegate = self
    self.presentViewController(viewController, animated: true, completion: nil)
}



//MARK: Protocol
func dismissViewController() {
    if let viewController = self.storyboard?.instantiateViewControllerWithIdentifier(String(SecondViewController)){
        self.presentViewController(viewController, animated: true, completion: nil)
    }
}

Code example with storyboard:

带故事板的代码示例:



回答by HSG

I think there is a mistake in your code where 'self' should be the presenting view controller to present 'vc', not 'vc' its self

我认为您的代码中有一个错误,其中“self”应该是呈现“vc”的呈现视图控制器,而不是“vc”自身

Your code

你的代码

self.dismissViewControllerAnimated(true, completion: {
                let vc = self.storyboard?.instantiateViewControllerWithIdentifier("OrderViewController")
                vc!.presentViewController(vc!, animated: true, completion: nil)
            })

Try this

尝试这个

self.dismissViewControllerAnimated(true, completion: {
                let vc = self.storyboard?.instantiateViewControllerWithIdentifier("OrderViewController")
                self.presentViewController(vc!, animated: true, completion: nil)
            })

hope this is helpful

希望这是有帮助的

回答by Code

let parent = self.parentViewController!

parent.dismissViewControllerAnimated(true, completion: {
            let vc = self.storyboard?.instantiateViewControllerWithIdentifier("OrderViewController")
            parent.presentViewController(vc!, animated: true, completion: nil)
        })

回答by pansora abhay

Here's a solution for Swift3

这是 Swift3 的解决方案

To present the ViewController

呈现 ViewController

let NotificationVC = self.storyboard?.instantiateViewController(withIdentifier: "NotificationVC") as! ExecutiveNotificationViewController

self.present(NotificationVC, animated: true, completion: nil)

To dismiss the ViewController:

要关闭 ViewController:

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