Shogan
Shogan

Reputation: 1195

MonoTouch - How do I declare UIPopoverController only when using an iPad device?

As I understand from reading documentation, UIPopoverControllers are only supported on the iPad. Therefore if you try to declare a variable as a UIPopoverController and run the app in the iPhone simulator or on an iPhone, you get an error such as:

UIPopoverController initWithContentViewController:] called when not running under UIUserInterfaceIdiomPad

So I have a universal monotouch app I am trying out, where I would like to use a UIPopoverController when the user is using an iPad, for the iPhone I have another solution.

This is how I am declaring it at the moment, but obviously running on the iPhone does not work, and I get the above error message.

public partial class IOPSCalculatorViewController : UIViewController
{

    static bool UserInterfaceIdiomIsPhone {
        get { return UIDevice.CurrentDevice.UserInterfaceIdiom == UIUserInterfaceIdiom.Phone; }
    }

    static bool UserInterfaceIdiomIsIPAD {
        get { return UIDevice.CurrentDevice.UserInterfaceIdiom == UIUserInterfaceIdiom.Pad; }
    }

    UIPopoverController DetailViewPopover = new UIPopoverController(new PopoverContentViewController());
}

How can I only declare the:

UIPopoverController DetailViewPopover = new UIPopoverController(new PopoverContentViewController());

part if the device is an iPad? I need the UIPopoverController to be in the public partial class so that I can access it publically in other places.

Upvotes: 1

Views: 1055

Answers (2)

poupou
poupou

Reputation: 43553

Instead of declaring and allocating in one line just split it in two lines. E.g.

UIPopoverController DetailViewPopover = null;

if (IOPSCalculatorViewController.UserInterfaceIdiomIsIPAD) {
    DetailViewPopover = new UIPopoverController (new PopoverContentViewController ());
}

That will also work if DetailViewPopover is a (public) field instead of an instance variable and, as long as the UIPopoverController constructor is not invoked, you won't be getting the error.

Upvotes: 1

iCoder4777
iCoder4777

Reputation: 1692

You need to find out what is your current device and write code for iphone and iPad as well. here is a snap of code that I've used in my case.

if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone)
     {
        //Do your coding here
     }
     else {
         if(![popoverController isPopoverVisible]){
             contact = [[[ContactViewController alloc] initWithNibName:@"ContactViewController_iPad" bundle:nil] autorelease];
             popoverController = [[[UIPopoverController alloc] initWithContentViewController:contact] retain];
             [popoverController setPopoverContentSize:CGSizeMake(400.0f, 400.0f)];
             [popoverController presentPopoverFromRect:CGRectMake(230, 860, 320.0f, 320.0f) inView:self.view permittedArrowDirections:UIPopoverArrowDirectionDown animated:YES];
             [popoverController release];
         }else{
             [popoverController dismissPopoverAnimated:YES];
         }
     }

hope this will help you. Happy Coding!!!

Upvotes: 0

Related Questions