Anya Alstreim Anya Alstreim - 4 months ago 67
iOS Question

popoverPresentationController is nil when used with a Navigation View Controller

This code was working properly before I embed my view in a navigation controller

if segue.identifier == "PopupInfo" {
let controller = segue.destinationViewController as! PopoverInfoViewController
controller.popoverPresentationController!.sourceRect = sender!.frame

}


However, after adding the navigation controller, this slightly edited code is no longer working because popoverPresenetationController is now nil! I need to set its sourceRect programmatically not with the storyboard because the sender is a control inside a cell in a table view

if segue.identifier == "PopupInfo" {
let navcontroller = segue.destinationViewController as! UINavigationController
let controller = navcontroller.topViewController as! PopoverInfoViewController
controller.popoverPresentationController!.sourceRect = sender!.frame

}


Note: controller is not nil, only its popoverPresentationController property

Answer

You embedded PopoverInfoViewController in a UINavigationController. That navigation controller is what is directly embedded in a UIPopoverPresentationController. It should now have a non-nil popoverPresentationController property, as it's the view controller that is directly embedded in the popover controller. The setting of these kind of parent view controller properties do not propagate past the first child view controller. This is why navcontroller.popoverPresentationController will be non-nil while any child of navcontroller will have nil for popoverPresentationController.

You should use navcontroller.popoverPresentationController!.sourceRect = sender!.frame