gpt4 book ai didi

ios - 从自定义 UIView 呈现 View Controller (xib 文件)

转载 作者:行者123 更新时间:2023-11-30 12:11:10 25 4
gpt4 key购买 nike

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

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

如何从 UIView 内部呈现另一个 ViewController。我无法弄清楚。

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

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

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

最佳答案

试试这个:

在 AccountInfoModal 文件中创建一个位于 AccountInfoModal 类之上的协议(protocol):

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 View 的位置设置 AccountInfoModal delegate = self 实例

关于ios - 从自定义 UIView 呈现 View Controller (xib 文件),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46043342/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com