是以编程方式设备iPhone或iPad

时间:2013-12-06 12:46:50

标签: ios iphone objective-c ipad

我正在开发全球应用程序(iPhone和iPad)。我正在为iPhone或iPad做不同的处理。但我看到像屏幕截图下面的崩溃。这个崩溃设备是iPhone,但运行了我为iPad写的代码。怎么可能。我写的区别iPhone和iPad的代码有问题吗? thanx

enter image description here

   -(IBAction)showSearchAirports:(id)sender{

        UIButton *tempButton=(UIButton*)sender;

        AirportSearch2 *airportsSearch=[[AirportSearch2 alloc] initWithNibName:@"AirportSearch" bundle:nil];

        if ([self isDeviceiPhone]) {
            [self presentViewController:airportsSearch animated:YES completion:NULL];
        }else{


                if (self.popOver) {
                    [self.popOver dismissPopoverAnimated:YES];
                    self.popOver = nil;

                }

                UINavigationController *navigationController = [[[UINavigationController alloc] initWithRootViewController:airportsSearch] autorelease];


                self.popOver=[[[UIPopoverController alloc] initWithContentViewController:navigationController] autorelease];

                self.popOver.delegate                    = self;
                [self.popOver setPopoverContentSize:CGSizeMake(285, 370)];

                //This line 481
                [self.popOver presentPopoverFromRect:tempButton.frame inView:self.view permittedArrowDirections:UIPopoverArrowDirectionRight animated:YES];


        }

}



-(BOOL)isDeviceiPhone{
    if([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone)
        return  TRUE;
    else
        return FALSE;
}

4 个答案:

答案 0 :(得分:4)

试试这个

if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
    {
         // The device is an iPad running iOS 3.2 or later.
         Return NO;
    }
    else
    {
         // The device is an iPhone or iPod touch.
         Return YES;
    }

答案 1 :(得分:0)

要检测设备类型,请使用以下方法:

+ (BOOL) isiPad {
#if (__IPHONE_OS_VERSION_MAX_ALLOWED >= 30200)
    if ([[UIDevice currentDevice] respondsToSelector: @selector(userInterfaceIdiom)])
        return ([UIDevice currentDevice].userInterfaceIdiom == UIUserInterfaceIdiomPad);
#endif
    return NO;
}


+(BOOL)isiPhone
{
    NSString *deviceType = [UIDevice currentDevice].model;
    if([deviceType isEqualToString:@"iPhone"])
        return YES;
    else
        return NO;
}

答案 2 :(得分:0)

只需在UI设备类中使用此宏定义,您也可以参考文档(https://developer.apple.com/LIBRARY/IOS/documentation/UIKit/Reference/UIDevice_Class/Reference/UIDevice.html) 只需使用像

这样的条件
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
{
    return yes;
}
else
    return No;

答案 3 :(得分:0)

您尚未向我们展示用于确定设备类型的代码,但这是您在运行时确定设备类型的一种方法。

if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
{
    // iPad-specific interface here
}
else
{
    // iPhone and iPod touch interface here
}