Popover视图控制器崩溃在iPad上

问题描述:

我试图显示一个视图控制器作为弹出窗口,它在iPhone上显示全屏时正常工作,但在iPad上崩溃。Popover视图控制器崩溃在iPad上

@IBAction func selectOpponentItems(_ sender: UIButton) { 

    let VC = storyboard?.instantiateViewController(withIdentifier: "ItemSelectionVC") as! ItemSelectionVC 
    // Error here 
    VC.delegate = self 
    VC.preferredContentSize = CGSize(width: UIScreen.main.bounds.width/2, height: UIScreen.main.bounds.height/2) 
    VC.modalPresentationStyle = UIModalPresentationStyle.popover 
    self.present(VC, animated: true, completion: nil) 
} 

func adaptivePresentationStyle(for controller: UIPresentationController) -> UIModalPresentationStyle { 
    return .none 
} 

该错误消息表示:

无法投 类型 '的UIViewController'(0x1b1e33f60)的值设为 'Overpower.ItemSelectionVC' (0x10005ad40)。

+0

什么是异常消息?它发生在哪条线上? – Paulw11

+0

@ Paulw11 plz查看编辑 –

+0

您的强制downcast失败,因为它不是'ItemSelectionVC',而只是一个'UIViewController' - 检查您的故事板。 – Paulw11

继@ TonyMkenu的回答,我加了这2行代码selectOpponentItems,和它的工作就像魔术:

VC.popoverPresentationController?.sourceView = sender 
VC.popoverPresentationController?.sourceRect = sender.bounds