簡體   English   中英

是以編程方式設備iPhone或iPad

[英]is device iPhone or iPad programmatically

我正在開發全球應用程序(iPhone和iPad)。 我正在為iPhone或iPad做不同的處理。 但我看到像屏幕截圖下面的崩潰。 這個崩潰設備是iPhone,但運行了我為iPad寫的代碼。 怎么可能。 我寫的區別iPhone和iPad的代碼有問題嗎? 感謝名單

在此輸入圖像描述

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

嘗試這個

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

要檢測設備類型,請使用以下方法:

+ (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;
}

只需在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;

您沒有向我們展示用於確定設備類型的代碼,但這里有一種可以在運行時確定設備類型的方法。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM