1

我是编程新手,对目标 c 非常陌生,所以我为任何不正确的术语道歉,或者可能使一个简单的概念比它更难。

我有两个非常简单的整数数组,我想根据当前设备进行切换

 int iPhoneDevice[2] = {320,410};
 int iPadDevice[2] = {768,1024};

如何根据当前设备将每个数组分配给单个变量???与此相同的想法。

if([[CurrentDevice] isEqualToString:@"iphone"]) {
        foo = iPhoneDevice[];      
    }else{
        foo = iPadDevice[];
    }

我需要能够在下一个基于肖像的条件语句中调用“foo”的两个值。在我看来,逻辑将与此类似。

 if(orientation == UIInterfaceOrientationPortrait || orientation == UIInterfaceOrientationPortraitUpsideDown) {
        bar = foo[1];
    }else {
        bar = foo[0];
    }

我有一个额外的嵌套 if 的预期结果,但我试图尽可能地压缩/清理我的代码。任何帮助,将不胜感激。提前致谢。

4

3 回答 3

2

我会这样处理:

CGSize iPhoneDeviceSize = CGSizeMake (320, 410);
CGSize iPadDeviceSize   = CGSizeMake (768, 1024);
CGSize foo;

int bar;

// assign to foo the appropriate device size based on the user interface idiom
foo = ([UIDevice currentDevice].userInterfaceIdiom == UIUserInterfaceIdiomPad) ?
      iPadDeviceSize : iPhoneDeviceSize;

// assign to bar the width of the screen based on the orientation of the device.
bar = ([UIDevice currentDevice].orientation == UIInterfaceOrientationPortrait || 
       [UIDevice currentDevice].orientation == UIInterfaceOrientationPortraitUpsideDown) ?
      size.width : size.height;

不确定这完全是您所追求的,但这是我要做的,基于您提供的代码。

于 2012-04-28T05:43:39.493 回答
1

要获取设备是移动设备还是 iPad(巧合的是,仅称为 phone 和 pad),请使用UIUserInterfaceIdiom( Pad/ Phone),如下所示:

if([UIDevice currentDevice].userInterfaceIdiom == UIUserInterfaceIdiomPad)
    //device is an iPad
   bar = foo[1];
else
    //Device is an iPhone/iPod Touch
    bar = foo[0];

或者你想设计它的样式。

于 2012-04-28T05:36:24.670 回答
1
int iPhoneDevice[2] = {320,410};
int iPadDevice[2] = {768,1024};

// I think this is the c syntax you're looking for
int *foo;

// CodaFi always has awesome amounts of knowledge handy
if ([UIDevice currentDevice].userInterfaceIdiom == UIUserInterfaceIdiomPad) {
   foo = iPadDevice;
} else {
   foo = iPhoneDevice;
}
于 2012-04-28T05:41:03.923 回答