【问题标题】:Dismiss and Present View Controller in Swift在 Swift 中关闭和呈现视图控制器
【发布时间】:2016-10-12 18:21:53
【问题描述】:

您好,我正在尝试展示一个视图控制器并关闭我当前的模态视图,但此代码不起作用

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

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

编辑:替换 vc!给自己

【问题讨论】:

  • 尝试使用 self.parentViewController?.presentViewController(vc!, animated: true, completion: nil)
  • @SandeepKumar 还是不行
  • 你是先展示还是先解散?

标签: ios swift uiviewcontroller


【解决方案1】:

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

你的代码

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

试试这个

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

希望对你有帮助

【讨论】:

  • 你是否检查一下 vc 是否为 nil。以防万一确保一切正常,然后我们才能追踪问题。
  • 它不是零,我的代码现在是 let vc = self.storyboard?.instantiateViewControllerWithIdentifier("OrderViewController") as! OrderViewController
【解决方案2】:

您必须获取呈现自我的视图控制器(当前视图控制器)。如果该视图控制器是 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)
}

【讨论】:

【解决方案3】:
let parent = self.parentViewController!

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

【讨论】:

    【解决方案4】:

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

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

    你可以使用协议让我们说如下:-

    在您的 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()
            }
        }
    

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

    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:


    【讨论】:

      【解决方案5】:

      这是 Swift3 的解决方案

      展示 ViewController

      let NotificationVC = self.storyboard?.instantiateViewController(withIdentifier: "NotificationVC") as! ExecutiveNotificationViewController
      
      self.present(NotificationVC, animated: true, completion: nil)
      

      关闭 ViewController:

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

      【讨论】:

        猜你喜欢
        • 2017-07-09
        • 1970-01-01
        • 2017-09-19
        • 1970-01-01
        • 2010-12-04
        • 1970-01-01
        • 2020-05-30
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多