#define
是一個預處理器定義。這意味着這是編譯中的第一件事情。它基本上只是在開始編譯之前將代碼中的定義粘貼到代碼中。但是由於你的if語句是在運行時執行的,而不是編譯時間,你可能需要將if語句改爲預處理器if(#if
,不推薦),或者改變寬度/高度以在運行時定義強力推薦)。這應該看起來像這樣:
int width, height;
if([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone){
width = X;
height = Y;
else {
width = A;
height = B;
}
然後從那以後,只需使用寬度和高度值爲您的寬度和高度。
如果你仍然要標註X,Y,A,B,而不是使用的#define(編譯時間常數),使用運行時常:
static const int iPhoneWidth = X;
static const int iPhoneHeight = Y;
static const int iPadWidth = A;
static const int iPadHeight = B;
if([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone){
width = iPhoneWidth;
height = iPhoneHeight;
else {
width = iPadWidth;
height = iPadHeight;
}
可能想使用'static const's而不是'#define's。 – 2012-02-22 06:03:44