Cannot get UIPopoverController to work

brbbrb AUMember ✭✭
edited October 2015 in Xamarin.iOS

Hi,

I am trying to use a popover where I have designed the ViewController in the Storyboard editor. The challenge I have is that none of the Views that I have in the storyboard ever get constructed or displayed. If I add them programatically in the ViewDidLoad method then it works just fine.

For example, this works:

partial class pBookmarkPopoverViewController : UIViewController
{
    public pBookmarkPopoverViewController (IntPtr handle) : base (handle)
    {
        return;
    }

    public pBookmarkPopoverViewController() : base()
    {
        return;
    }

    public override void ViewDidLoad()
    {           
        base.ViewDidLoad ();
        // Perform any additional setup after loading the view, typically from a nib.

        var label = new UILabel (new CGRect (0, 0, 320, 320));
        label.Text = "POP!";
        label.Font = UIFont.BoldSystemFontOfSize (100);
        label.TextAlignment = UITextAlignment.Center;
        label.TextColor = UIColor.Red;
        Add (label);

        return;
    }
}

Naturally the ViewController designed in the Storyboard editor looks pretty straight forward. All I added was the empty constructor:

partial class PopoverContentViewController : UIViewController
{
    public PopoverContentViewController (IntPtr handle) : base (handle)
    {
    }

    public PopoverContentViewController () : base()
    {
    }

    public override void ViewDidLoad()
    {           
        base.ViewDidLoad ();
        // Perform any additional setup after loading the view, typically from a nib.

        return;
    }

    public override void ViewDidAppear( bool animated)
    {
        base.ViewDidAppear( animated );
    }
}

When ViewDidLoad() and ViewDidAppear() are called all the Views in the ViewController are null.

Here is how I am instantiating the popups (both the one that works and the one that does not):

    partial void bGearButton_Activated (UIBarButtonItem sender)
    {
        // Open the popup.
        PopoverContentViewController popoverController = new PopoverContentViewController();
        UIPopoverController popover = new UIPopoverController( popoverController );
        popover.Delegate = new PopoverDelegate();
        popover.PopoverContentSize = new CoreGraphics.CGSize( 320, 320 );
        popover.PresentFromBarButtonItem( bGearButton, UIPopoverArrowDirection.Any, true);

        return;
    }

I have spent most of the day trying to find some details on this but have drawn a blank. Is there a trick to this? or do you need to build the ViewControllers that are used in popups programatically?

Thanks

Bryon

Best Answer

Answers

  • brbbrb AUMember ✭✭
    edited October 2015

    Thanks Adam, That fixed it.
    For those that follow me - this code was what fixed it up. Note that:
    1. bGearButton_Activated() is called when a BarButton is pressed on the main ViewController.
    2. InstantiateViewController() means you do not need to add the empty constructor I had added to the ViewController that will be presented in the popup.

        partial void bGearButton_Activated (UIBarButtonItem sender)
        {
            // Open the popup.
            UIViewController popoverController = Storyboard.InstantiateViewController("PopoverContentViewController");
            UIPopoverController popover = new UIPopoverController( popoverController );
            popover.Delegate = new PopoverDelegate();
            popover.PopoverContentSize = new CoreGraphics.CGSize( 300, 300 );
            popover.PresentFromBarButtonItem( bGearButton, UIPopoverArrowDirection.Any, true);
    
            return;
        }
    
Sign In or Register to comment.