返回功能目标C

问题描述:

我有一个问题我想调用一个函数,并使用我从函数中获得的价值,这里是我的函数的代码返回功能目标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'吗? “ – *foe

+3

”甚至没有调用_function_“在这里,_function_是对象,类还是方法?尝试使用更好的名字并解释究竟是什么问题。 – sidyll

+0

是否有可能你没有真正创建一个'Function'的实例,这意味着运行时变量'function'是'nil'? – Tommy

您没有传入参数。

你应该有这样的事情:

[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

既然你不Class1中使用任何实例变量,你应该可能实施CellWidth作为class factory method使用+而不是-定义。您可以直接使用类名称[Class1 CellWidth]直接调用它。

+0

同意,如果这将是在所有的Class1的一样,那么它应该是一个类的方法,而不是一个实例方法 – ColdLogic