从自定义UIView(xib文件)呈现视图控制器
问题描述:
我有一个自定义UIView
子类(AccountInfoModal
),它通过将其添加为子视图而显示在ViewController
的顶部。 AccountInfoModal
有一个按钮,这应该导致另一个ViewController
被呈现。 初始化其他ViewController
不是问题。呈现它。从自定义UIView(xib文件)呈现视图控制器
由于AccountInfoModal
不是ViewController
我不能使用.present(viewControllerToPresent...)
。
如何从UIView中呈现另一个ViewController。我想不明白。
编辑: 我已经实现了这个建议:
let underlyingVC = UIApplication.shared.keyWindow?.rootViewController
underlyingVC?.performSegue(withIdentifier: "SEGSignInViewController", sender: nil)
但它会导致崩溃的最后一行(包无法加载NIB:...)。
答
尝试此:
在AccountInfoModal文件创建以上AccountInfoModal类的协议:
protocol AccountInfoModalDelegate : class {
func willPresentingViewController(){
}
AccountInfoModal类的内部限定delegate
变量:
weak var delegate?
按钮动作调用内部:
delegate?.willPresentingViewController()
在视图控制器文件里加入以下内容:
extension ViewController : AccountInfoModalDelegate {
func willPresentingViewController(){
let vc = AnotherVC()
self.present(vc, animated : true, completion : nil)
}
}
最后一步,在您致电提出AccountInfoModal视图AccountInfoModal delegate = self
的设置实例
答
给AccountInfoModal
的属性,containingViewController。当您将AccountInfoModal
视图安装为子视图时进行设置。然后用它来呈现其他视图控制器。
答
的最简单的方式 - 就目前认为的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