【问题标题】:Moving back to parent view controller or dismissing child view controller移回父视图控制器或关闭子视图控制器
【发布时间】:2020-05-19 18:41:17
【问题描述】:

单击按钮时,我正在从一个控制器导航到另一个控制器。我最初是在做self.present(nextViewController, animated:true, completion:nil),它覆盖了设备上的整个窗口。由于我希望它的大小与我的主(呈现)控制器一样大,所以下面是我现在用来呈现新控制器的代码。

从父控制器导航到 NewViewController:

@IBAction func doneButtonAction(_ sender: Any) {
        let storyBoard : UIStoryboard = UIStoryboard(name: "Main", bundle:nil)
        let nextViewController = storyBoard.instantiateViewController(withIdentifier: "MyViewID") as! NewViewController
        nextViewController.view.frame = self.view.bounds
        nextViewController.willMove(toParent: self)
        self.view.addSubview(nextViewController.view)
        self.addChild(nextViewController)
        nextViewController.didMove(toParent: self)
        //self.present(nextViewController, animated:true, completion:nil)
    }

这工作正常并将我导航到 NewViewController 控制器。但是现在从 NewViewController 控制器,我希望回到这个我的关闭逻辑不起作用的父控制器。

从 NewViewController 回到父控制器

@IBAction func backButtonAction(_ sender: Any) {
    self.dismiss(animated: true, completion: nil)
}

我错过了什么?如何返回父控制器?

【问题讨论】:

    标签: ios uiviewcontroller


    【解决方案1】:

    要在整个窗口中显示,您实际上需要做的是修改nextViewControllermodalPresentationStyle 属性。

    let storyBoard : UIStoryboard = UIStoryboard(name: "Main", bundle:nil)
    let nextViewController = storyBoard.instantiateViewController(withIdentifier: "MyViewID") as! NewViewController
    nextViewController.modalPresentationStyle = .fullScreen
    present(nextViewController, animated: true)
    

    将此代码添加到doneButtonAction 并以旧方式呈现。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-05-19
      • 2014-08-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多