从自定义UIView(xib文件)呈现视图控制器

时间:2017-09-04 19:56:26

标签: ios swift model-view-controller uiview

我有一个自定义UIView子类(AccountInfoModal),它通过将其添加为子视图而显示在ViewController之上。 AccountInfoModal有一个按钮,应该会显示另一个ViewController。 初始化其他ViewController并不是问题所在。提出它是。

由于AccountInfoModal不是ViewController,我无法使用.present(viewControllerToPresent...)

如何从UIView内部呈现另一个ViewController。我无法理解。

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

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

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

3 个答案:

答案 0 :(得分:0)

试试这个:

在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视图

答案 1 :(得分:0)

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

答案 2 :(得分:0)

最简单的方法 - 从根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
相关问题