2013-03-14 54 views
2

我試圖在不使用筆記本的情況下編寫應用程序筆尖,所有事情我都會以編程方式進行。iOS - 在不使用筆尖的情況下支持iPad和iPhone

現在的問題是,我該如何支持iPadiPhone?很顯然,我不能

if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad) { 
    // load iPad nib 
} else { 
    // load iPhone nib 
} 

做到這一點如果我創建2個ViewControllers,則IBAction將是多餘的。

有什麼建議嗎?

+0

你可以在上面的[UIDevice currentDevice]條件中單獨設置幀,並且執行通常的功能.....我認爲沒有使用這個,你不能這樣做..... – Venkat 2013-03-14 05:16:12

+1

如果一切都是編程的,那麼爲什麼你想創建兩個視圖控制器?一個就足夠了 – 2013-03-14 05:16:59

+0

您是否在創建項目時選擇了通用應用程序。 – Dipendra 2013-03-14 05:24:48

回答

0
CGFloat height = [UIscreen mainScreen].bounds.size.height; 

if(height==568.00 || height == 480.0) 
{ 
//For iphone 5 and iphone 4 

} 
else 
{ 
//For ipad 
} 
+0

這不適用於iphone 5和iphone 4,因爲'height == 568.00 && height == 480.0'的條件是* always *'false'。 – dasblinkenlight 2013-03-14 05:36:30

+0

高度不能都是568(應該是548 BTW)和480,你想要「||」沒有「&&」。 – rdelmar 2013-03-14 05:36:40

+0

好的我編輯了我的答案 – Rushabh 2013-03-14 05:38:39

0

你可以在你的AppDelegate使用此代碼

- (BOOL) isPad() 
{ 

    if(UI_USER_INTERFACE_IDIOM) 
    { 
     return (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad); 
    } 
    else 
    { 
     return NO; 
    } 

} 

這個鏈接給出了有關的成語一些信息http://developer.apple.com/library/ios/#documentation/uikit/reference/UIKitFunctionReference/Reference/reference.html#//apple_ref/c/macro/UI_USER_INTERFACE_IDIOM

OR

您可以查看屏幕的高度和寬度要知道它的iPhone或iPad是否是

CGRect screen = [[UIScreen mainScreen] bounds]; 
CGFloat width = CGRectGetWidth(screen); 
CGFloat height = CGRectGetHeight(screen); 
3

您應該只需找出applicationDidFinishLaunching中的設備類型,然後爲每個設備加載單獨的控制器。但是,如果你只想對所有設備的單一實現,就檢查這樣的:

if(UI_USER_INTERFACE_IDIOM()==UIUserInterfaceIdiomPad) 
{ 
    //do some iPad stuff 
} 
else 
{ 
    CGFloat screenH = [UIScreen mainScreen].bounds.size.height; 

    if([UIScreen mainScreen].scale == 2.f && screenH == 568.0f) 
    { 
     //do iPhone 5 stuff 
    } 
    else 
    { 
     //do iPhone 4S and iPhone 4 stuff 

     //the dimensions are the same however, if you want to do iPhone 4S specific stuff 
     // you'll need to do additional checks against video resolution or other differences etc 
    } 
}  
0

如果你不使用任何筆尖,一切編程DOEN你不想創建iPhone和iPad兩種查看控制器。請記住,不要依賴任何靜態值。即你的計算應根據self.view.bounds這樣的事情來做(我的意思是創建視圖/子視圖)。那麼,如果僅支持iPad的在一些特定的功能做檢查

if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad) 

一個視圖控制器完成所有的工作適合你。

+0

如果iPad的外觀相同,我可以這樣問iPhone,但是如果iPad有側邊欄,該怎麼辦?那麼有不同的意見 – 2013-03-14 06:05:01

+0

在這種情況下,最好創建兩個視圖控制器。根據設備呈現視圖控制器。或者您可以相應地在視圖控制器的loadView方法中加載不同的視圖。無論如何,你必須使用代表來處理動作 – 2013-03-14 06:15:06

+0

你的意思是爲1 ViewController創建2個視圖?但在這種情況下,我必須實施2代表權? – 2013-03-14 07:27:14

相關問題