5
votes

As title said, I want to show another UIViewController from an existing UIViewController which is hosted in UIPopoverController. I tried the following method:

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

However, the secondViewController is shown in the main view controller, instead of the popover controller.

In this post somebody mentions that it cannot be done and it violates the HIG. However, I have seen this in other apps (e.g. Yahoo! Email) if I'm not mistaken.

I'm also thinking about another approach: If I could create a UINavigationController within the popover context, it might work by just adding new ViewController to the NavigationController. But how?

1
As far as I know you can't and you probably shouldn't. You can simulate something like this by invoking present modal view controller from a non-popup vc, and use something like PresentFromRectCandide

1 Answers

3
votes

Remember that UINavigationController derives from UIViewController.

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

Usage:

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