PopoverPresentationController以零开头

创建了一个SingleViewApplication,我已经放置了一个按钮。

现在点击按钮我需要将tableView显示为popover。 TableViewController是在xib中创建的。

问题是tableViewController.popoverPresentationController始终为零,参见下面的代码

let filterVC = TableViewController(nibName: "TableViewController", bundle: nil) var filterDistanceViewController = UINavigationController(rootViewController: filterVC) filterDistanceViewController.preferredContentSize = CGSize(width: 300, height: 200) let popoverPresentationViewController = filterDistanceViewController.popoverPresentationController popoverPresentationViewController?.permittedArrowDirections = .any if let pop = filterDistanceViewController.popoverPresentationController { pop.delegate = self } 

在上面的代码filterDistanceViewController.popoverPresentationController总是为零

任何正确方向的提示都将受到高度赞赏。

您没有呈现任何内容,因此您需要在当前viewcontroller上显示popoverPresentationViewController ,例如:

 @IBAction func importantButtonPressed(_ sender: UIButton) { let tableViewController = UITableViewController() tableViewController.modalPresentationStyle = .popover present(tableViewController, animated: true, completion: nil) if let pop = tableViewController.popoverPresentationController { pop.delegate = self } } 

你可以在下面做。

 @IBAction func popoverBtnPressed(_ sender: Any) { let vc2 = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "ViewController2") vc2.modalPresentationStyle = .popover vc2.popoverPresentationController?.delegate = self vc2.popoverPresentationController?.barButtonItem = popoverBtn vc2.popoverPresentationController?.sourceRect = .zero present(vc2, animated: true, completion: nil) } 

在VC上设置modalPresentationStyle之前, popoverPresentationController属性将为nil 。 确保在访问之前设置modalPresentationStyle