iOS - 在不使用笔尖的情况下支持iPad和iPhone

iOS - 在不使用笔尖的情况下支持iPad和iPhone

问题描述:

我试图在不使用笔记本的情况下编写应用程序笔尖,所有事情我都会以编程方式进行。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

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

你可以在你的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); 

您应该只需找出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 
    } 
}  

如果你不使用任何笔尖,一切编程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