Возврат Функции цель c - PullRequest
       3

Возврат Функции цель c

0 голосов
/ 22 ноября 2011

У меня есть одна проблема: я хочу вызвать функцию и использовать значение, полученное из этой функции, вот код моей функции

-(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 вот так - (двойной) CellWidth;

Теперь я хочу использовать его в Class2

код в Class2.h

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

Class2.m

Я хочу использовать это

self.TableView.rowHeight = [class1 CellWidth];

Но CellWidth не вызывается, и я не получаю ширину.

Ответы [ 2 ]

2 голосов
/ 22 ноября 2011

Вы не передаете параметр.

У вас должно быть что-то вроде этого:

[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
1 голос
/ 22 ноября 2011

Поскольку вы не используете какие-либо переменные экземпляра в Class1, вам, вероятно, следует реализовать CellWidth как фабричный метод класса , определенный с использованием + вместо -.

Теперь вам не нужно добавлять его как свойство в Class2, но вы можете вызвать его напрямую, используя имя класса: [Class1 CellWidth].

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...