MonoTouch for iPad: how to show another UIViewController in a UIPopoverController?

As the name says, I want to show another UIViewController from an existing UIViewController that is hosted in a UIPopoverController. I tried the following method:

_secondViewController = new SecondViewController();
this.ModalPresentationStyle = UIModelPresentationStyle.CurrentContext;
this.ModelInPopover = true;
this.PresentModelViewController(_secondViewController, true);

However, the secondViewController is displayed in the main view controller, and not in the popover controller.

In this post, someone mentions that this is not possible and that violates HIG. However, I have seen this in other applications (e.g. Yahoo! Email), if I'm not mistaken.

I am also thinking of a different approach: if I could create a UINavigationController in the context of a popover, this might work just by adding a new ViewController to the NavigationController. But how?

+5
source share
1 answer

Remember that the UINavigationController comes from the UIViewController.

So you can use the controller contained in UIPopover like any other container ... in this case, it is best to use the UINavigationController inside the UIPopover to display the ViewControllers.

Using:

var _NavController = new NavController();

Popover = new UIPopoverController(_NavController);
Popover.PopoverContentSize = new SizeF(..., ...);

Popover.PresentFromRect(...);

NavController:

public class NavController : UINavigationController
{
    UIViewController _FirstViewController; 
    UIViewController _SecondViewController;

    public NavController()
        : base()
    {
    }

    public override void LoadView()
    {
        base.LoadView();

        _FirstViewController = new UIViewController();

        // Initialize your originating View Controller here.
        // Only view related init goes here, do everything else in ViewDidLoad()
    }

    public override void ViewDidLoad()
    {
        base.ViewDidLoad();

        // When a button inside the first ViewController is clicked
        // The Second ViewController is shown in the stack.

        _FirstViewController.NavButton.TouchUpInside += delegate {
            PushSecondViewController(); 
        };

        this.PushViewController(_FirstViewController, true);
    }

    public void PushSecondViewController()
    {
        _SecondViewController = new UIViewController();
        this.PushViewController(_SecondViewController, true);
    }
}
+3
source

All Articles