【问题标题】:Presenting a View Controller from an custom UIView (xib-file)从自定义 UIView(xib 文件)呈现视图控制器
【发布时间】:2018-02-13 01:30:52
【问题描述】:

我有一个自定义的 UIView 子类 (AccountInfoModal),它通过将其添加为子视图呈现在 ViewController 之上。 AccountInfoModal 有一个按钮,应该会导致另一个 ViewController 出现。 初始化其他ViewController 不是问题。介绍它是。

由于AccountInfoModal 不是ViewController 我不能使用.present(viewControllerToPresent...)

如何从 UIView 中呈现另一个 ViewController。我想不通。

编辑: 我已经实施了这个建议:

let underlyingVC = UIApplication.shared.keyWindow?.rootViewController
underlyingVC?.performSegue(withIdentifier: "SEGSignInViewController", sender: nil)

但它会导致最后一行崩溃(无法在包中加载 NIB:...)。

【问题讨论】:

    标签: ios swift model-view-controller uiview


    【解决方案1】:

    最简单的方法 - 只需从根 UIViewController 呈现 UIViewController:

    let vc = UIViewController() //Your UIViewController
    let rootVC = UIApplication.shared.keyWindow?.rootViewController //UIViewController from where you will present your UIViewController
    rootVC?.present(vc, animated: true, completion: nil)    //Present method
    

    【讨论】:

      【解决方案2】:

      AccountInfoModal一个属性,包含ViewController。在将AccountInfoModal 视图安装为子视图时设置它。然后用它来呈现另一个视图控制器。

      【讨论】:

        【解决方案3】:

        试试这个:

        在 AccountInfoModal 文件中,在 AccountInfoModal 类之上创建一个协议:

        protocol AccountInfoModalDelegate : class {
        func willPresentingViewController(){
        }
        

        在 AccountInfoModal 类中定义 delegate 变量:

        weak var delegate?
        

        内部按钮动作调用:

        delegate?.willPresentingViewController()
        

        在 ViewController 文件中添加以下内容:

        extension ViewController :  AccountInfoModalDelegate {
            func willPresentingViewController(){
            let vc = AnotherVC()
            self.present(vc, animated : true, completion : nil)
             }
        }
        

        最后一步,在您调用的地方设置 AccountInfoModal delegate = self 的实例以呈现 AccountInfoModal 视图

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2021-11-07
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2012-03-03
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多