gpt4 book ai didi

ios - 返回函数 objective-c

转载 作者:行者123 更新时间:2023-11-28 20:41:49 24 4
gpt4 key购买 nike

我有一个问题,我想调用一个函数并使用我从该函数获得的值,这是我的函数的代码

-(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 中这样声明 -(双)单元格宽度;

现在我想在Class2中使用它

Class2.h中的代码

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

Class2.m

我想用这个

self.TableView.rowHeight = [class1 CellWidth];

但是 CellWidth 没有被调用,我也没有收到宽度。

最佳答案

您没有传递参数。

你应该有这样的东西:

[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

关于ios - 返回函数 objective-c ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8231296/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com