如何将字符串值从一个.m文件传递到另一个.m文件

时间:2012-03-27 13:49:35

标签: iphone objective-c ios uiviewcontroller nsstring

嗨,我是客观c的新手。我有一个.m文件,其中我有要传递的字符串。

NSString *passedMonth;

我正在传递它

KLTile *kltil = [[KLTile alloc] inittempMonth:passedMonth];

temp month是其他.m文件中的字符串

-(id)inittempMonth:(NSString *)tem{

        tempMonth = [[NSString alloc]init];
        self.tempMonth = tem;
        NSLog(@" temp month....%@",self.tempMonth);

    return self;
}

init中的日志正在给出输出但init方法之外的相同日志不起作用......

我想在-(id)inittempMonth:(NSString *)tem{之外使用tempMonth字符串... 有没有办法在init方法之外使用字符串...

1 个答案:

答案 0 :(得分:3)

您的init方法错误。因此,您应该将其修改为:

- (id)initWithTempMonth:(NSString *)tem{
{
    self = [super init]; // This line is important
    if (self) {
        self.tempMonth = tem;
        NSLog(@" temp month....%@",self.tempMonth);
    }
    return self;
}

另外,不要忘记在tempMonth文件中将.h声明为保留属性:

@property (nonatomic, retain) NSString *tempMonth;

或者如果您使用的是ARC:

@property (nonatomic, strong) NSString *tempMonth;

然后你可以像这样记录属性的值:

KLTile *kltil = [[KLTile alloc] inittempMonth:passedMonth];
NSLog(@"Temp month: %@", kltil.tempMonth);

相关问题