barbutton popover style segue's anchor

i have bar buttons added to the nav bar programatically and i would like to set the relevant one as an anchor to a popover style sugue. this button is obviously not visible in interface builder and i dont see any property for an anchor in UIStoryboardSegue class.


any idea how to do this?


one more question... how will the popover sgue present a view controler on an iphone, since iphones aren't allowed popovers.


thanks.

Neerav

You can override and implement -prepareForSegue: in the source view controller. That method is called when a segue begins and it's where you prepare or customize the segue, or prepare the destination view controller, such as passing it a data object.


In your case, you need to get the destination view controller (the one that'll appear as a popover), cast it and tell it which UIBarButtonItem to use as an anchor.


  override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        if let vc = segue.destinationViewController as? UIViewControllerSubclassChangeToYourDestinationClass {
            vc.popoverPresentationController?.barButtonItem = yourBarButtonItem
        }
    }


As for the second question, iPhones are allowed to present popovers, that changed in iOS 8. The question for your app is whether or not it makes sense to so-do, given the constrained screen size. Normally they would present as a full-page on iPhone, but you can conform your source view controller to UIAdaptivePresentationControllerDelegate, then override the following method so a modal presentation doesn't occur:


    func adaptivePresentationStyleForPresentationController(controller: UIPresentationController) -> UIModalPresentationStyle {
        return .None
    }


If you implement that adaptive presentation delegate, then you'd also need to update -prepareForSegue: such that the destination view controller ("vc", above) knows the current class is its adaptive presentation delegate:


    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        if let vc = segue.destinationViewController as? UIViewControllerSubclassChangeToYourDestinationClass {
            vc.popoverPresentationController?.barButtonItem = yourBarButtonItem
            vc.popoverPresentationController?.delegate = self
        }
    }


There's a WWDC 2014 talk about new view controller adaptive styles, the concept is explained in there. In that video there's a comment about setting the delegate *after* presenting the new view controller, which isn't now the case, you set the delegate before presenting, but I think it would be worth an hour or so to give that video a watch through to learn more about these techniques.


Hope this helps.

NorthCoaster, thanks a lot for your reply. Xcode requires the popover anchor at compile time. It throws an error otherwise.


I would like to request you to put the above code in objC. sorry for the trouble. havent switched to swift yet.

@interface ViewController () <UIAdaptivePresentationControllerDelegate>

@end

@implementation ViewController

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    UIViewControllerSubclassChangeToYourDestinationClass *vc = segue.destinationViewController;
    vc.popoverPresentationController.barButtonItem = yourBarButtonItem;
    vc.popoverPresentationController.delegate = self;
}

- (UIModalPresentationStyle)adaptivePresentationStyleForPresentationController:(UIPresentationController *)controller {
    return UIModalPresentationNone;
}

@end
barbutton popover style segue's anchor
 
 
Q