PopoverPresentationController coming as nil

℡╲_俬逩灬. 提交于 2019-12-12 21:25:44

问题


Created a SingleViewApplication in that I have placed a button.

Now clicking on button I need to display a tableView as popover. The TableViewController is created in xib.

The issue is tableViewController.popoverPresentationController always comes as nil see below code

     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
    }

in above code filterDistanceViewController.popoverPresentationController is always coming as nil

Any hint in right direction will be highly appreciated.


回答1:


You are not presenting anything, so you need to present the popoverPresentationViewController on the current viewcontroller, for example:

@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
        }

    }



回答2:


Until you've set a modalPresentationStyle on your VC, the popoverPresentationController property will be nil. Ensure that you set the modalPresentationStyle before accessing.




回答3:


You may do like below.

@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)
}


来源:https://stackoverflow.com/questions/44666281/popoverpresentationcontroller-coming-as-nil

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!