使用程式判斷目前使用的機型是IPHONE還是IPAD



typedefenum {
#if __IPHONE_3_2 <= __IPHONE_OS_VERSION_MAX_ALLOWED
    UIUserInterfaceIdiomPhone,           // iPhone and iPod touch style UI
    UIUserInterfaceIdiomPad,             // iPad style UI
#endif
} UIUserInterfaceIdiom;

/* The UI_USER_INTERFACE_IDIOM() macro is provided for use when deploying to a version of the iOS less than 3.2. If the earliest version of iPhone/iOS that you will be deploying for is 3.2 or greater, you may use -[UIDevice userInterfaceIdiom] directly.
 */
#define UI_USER_INTERFACE_IDIOM() ([[UIDevice currentDevice] respondsToSelector:@selector(userInterfaceIdiom)] ? [[UIDevice currentDevice] userInterfaceIdiom] : UIUserInterfaceIdiomPhone)

UIDevice的property userInterfaceIdiom

The style of interface to use on the current device. (read-only)
@property (nonatomic, readonly)  UIUserInterfaceIdiom userInterfaceIdiom

在AppDelegate裡,通過獲取UIDevice的userInterfaceIdiom屬性,可以檢測到當前程序是運行在iphone或者是ipad上,分別加載相應的視圖,就能在兩種設備中正常的運行起來。例子如下:


UIDevice* device=[UIDevice currentDevice];
window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
if (device.userInterfaceIdiom==UIUserInterfaceIdiomPhon e) {
IPhoneViewController*  IPhoneViewController1=[[IPhoneViewController  alloc]init];
window.rootViewController=  IPhoneViewController1;
[IPhoneViewController1  release];
}
else if (device.userInterfaceIdiom==UIUserInterfaceIdiomPad) 
{
IPadViewController*  IPadViewController1=[[IPadViewController  alloc]init];
window.rootViewController=  IPadViewController1;
[IPadViewController1  release];
}


留言

這個網誌中的熱門文章

Office 2021 離線安裝封裝與KMS啟動步驟

Ollama使用心得與模型導入教學

ARC下NSMutableDictionary 無法使用retainCount