2017-09-04 87 views
0

我有一个自定义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:...)。

回答

0

尝试此:

在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的设置实例

0

AccountInfoModal的属性,containingViewController。当您将AccountInfoModal视图安装为子视图时进行设置。然后用它来呈现其他视图控制器。

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 
相关问题