2011-11-22 52 views
0

我有一個問題我想調用一個函數,並使用我從函數中獲得的價值,這裏是我的函數的代碼返回功能目標C

-(double)CellWidth{ 
double width; 


if([[UIDevice currentDevice] orientation] == UIDeviceOrientationPortrait || [[UIDevice currentDevice]orientation] == UIDeviceOrientationPortraitUpsideDown){ 
    NSLog(@"Device is now in Portrait Mode"); 
    width = 153.6; 
} 
if ([[UIDevice currentDevice] orientation] == UIDeviceOrientationLandscapeLeft || [[UIDevice currentDevice] orientation] == UIDeviceOrientationLandscapeRight) { 
    NSLog(@"Device is now in LandscapeLeft Mode "); 
    width = 204.6; 
} 

return width; 
} 

該功能是在Class1.m但我也在Class1.h中聲明,如下所示 - (double)CellWidth;

現在我想在Class2.h

#import "Class1.h" 
@interface ... 
{ 
Class1 *class1; 
} 
@property (nonatomic,release) Class1 *class1; 

Class2.m

使用它在Class2中

代碼,我想用這個

self.TableView.rowHeight = [class1 CellWidth]; 

但CellWidth沒有被調用,我沒有收到寬度。

+0

你不應該將'UIInterfaceOrientation'傳遞給'CellWidth'嗎? 「 – trojanfoe

+3

」甚至沒有調用_function_「在這裏,_function_是對象,類還是方法?嘗試使用更好的名字並解釋究竟是什麼問題。 – sidyll

+0

是否有可能你沒有真正創建一個'Function'的實例,這意味着運行時變量'function'是'nil'? – Tommy

回答

2

您沒有傳入參數。

你應該有這樣的事情:

[self setClass1:[[[Class1 alloc] init] autorelease]]; 

UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation]; 

//This 
[[self horizontalTableView] setRowHeight:[class1 CellWidth:orientation]]; 

當前的實現並不調用該函數的原因是因爲你沒有告訴它調用該函數。你告訴它叫[function CellWidth][function CellWidth:orientation]

基於您的反饋,你實際上似乎想要更多的東西是這樣的:

-(double)CellWidth { 
    double width = 0; 

    UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation]; 

    if(orientation == UIInterfaceOrientationPortrait) { 
     NSLog(@"PORTRAIT"); 
     width = 153.6; 
    } else if(orientation == UIInterfaceOrientationLandscapeLeft || 
       orientation == UIInterfaceOrientationLandscapeRight) { 
     NSLog(@"LANDSCAPE"); 
     width = 204.6; 
    } 

    return width; 

} 

然後在您的實現,在Class2.m:

[self setClass1:[[[Function alloc] init] autorelease]]; 

//This 
[[self horizontalTableView] setRowHeight:[class1 CellWidth]]; 

爲了使這更清楚,我要嘗試收拾這個...

CoolCellInfo.h

@interface CoolCellInfo : NSObject { 

} 

-(double)cellWidth; 

@end 

CoolCellInfo.m

@implementation CoolCellInfo 

-(id)init { 
    self = [super init]; 

    if(self) { 

    } 

    return self; 
} 

-(double)cellWidth { 
    double width = 0; 

    UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation]; 

    if(orientation == UIInterfaceOrientationPortrait) { 
     NSLog(@"PORTRAIT"); 
     width = 153.6; 
    } else if(orientation == UIInterfaceOrientationLandscapeLeft || 
       orientation == UIInterfaceOrientationLandscapeRight) { 
     NSLog(@"LANDSCAPE"); 
     width = 204.6; 
    } 

    return width; 
} 

@end 

CoolCellUser.h

#import "CoolCellInfo.h" 

@interface CoolCellUser : NSObject { 
    CoolCellInfo *cellInfo; 
} 

@property (nonatomic, retain) CoolCellInfo *cellInfo; 

@end; 

CoolCellUser.m

@implementation CoolCellUser 
@synthesize cellInfo; 

-(id) init { 
    self = [super init]; 

    if(self) { 
     double width = [[self cellInfo] cellWidth]; 

     NSLog(@"Omg cell width = %f", width); 
    } 

    return self; 
} 

#pragma mark Lazy Loader 
-(CoolCellInfo *)cellInfo { 
    if(cellInfo == nil) { 
     [self setCellInfo:[[[CoolCellInfo alloc] init] autorelease]]; 
    } 

    return cellInfo; 
} 

@end 
+0

,但我想知道哪個方向是設備內部的單元寬度。我如何在cellwidth內部做到這一點?我試着發送方向信息,但仍然沒有調用信元寬度。謝謝 – Alfonso

+0

然後你需要一個不同的結構。要檢查方向,請使用'[[UIApplication sharedApplication] statusBarOrientation]'。還要確保你正在實例化你的變量,否則它是零,這個函數永遠不會被調用。 – ColdLogic

+0

謝謝這工作! – Alfonso

1

既然你不Class1中使用任何實例變量,你應該可能實施CellWidth作爲class factory method使用+而不是-定義。您可以直接使用類名稱[Class1 CellWidth]直接調用它。

+0

同意,如果這將是在所有的Class1的一樣,那麼它應該是一個類的方法,而不是一個實例方法 – ColdLogic