English 中文(简体)
职务回归目标c
原标题:Returning Functions objective c

I have one problem I want to call a function and use the value I get from that function, here is the code of my function

-(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;
}

that function is in Class1.m but I also declared in Class1.h like this -(double)CellWidth;

我现在想在2级中使用。

第2类:h

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

2级

我想利用这一点。

self.TableView.rowHeight = [class1 CellWidth];

但是,这支民兵组织没有被召唤,我没有得到宽恕。

最佳回答

你们不会通过一个参数。

You should have something like this:

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

UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation];

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

The reason your current implementation isn t calling that function is because you aren t telling it to call that function. You re telling it to call [function CellWidth] not [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;

}

then in your implementation, in Class2.m:

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

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

为了更清楚地说明这一点,我将尝试并清理这件事。

CoolCellInfo.h

@interface CoolCellInfo : NSObject {

}

-(double)cellWidth;

@end

gilCellInfo.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
问题回答

Since you aren t using any instance variables in Class1, you should probably implement CellWidth as a class factory method defined using + rather than -.

Now you don t need to add it as a property in Class2, but you can call it directly using the class name: [Class1 CellWidth].





相关问题
List Contents of Directory in a UITableView

I am trying to list the contents of Ringtones directory in a TableView, however, I am only getting the last file in the directory in ALL cells, instead of file per cell. This is my code: - (...

iPhone NSUserDefaults persistance difficulty

In my app i have a bunch of data i store in the NSUserdefaults. This information consists of an NSObject (Object1) with NSStrings and NSNumbers and also 2 instances of yet another object (Object2). ...

Writing a masked image to disk as a PNG file

Basically I m downloading images off of a webserver and then caching them to the disk, but before I do so I want to mask them. I m using the masking code everyone seems to point at which can be found ...

Resize UIImage with aspect ratio?

I m using this code to resize an image on the iPhone: CGRect screenRect = CGRectMake(0, 0, 320.0, 480.0); UIGraphicsBeginImageContext(screenRect.size); [value drawInRect:screenRect blendMode:...

Allowing interaction with a UIView under another UIView

Is there a simple way of allowing interaction with a button in a UIView that lies under another UIView - where there are no actual objects from the top UIView on top of the button? For instance, ...

热门标签