Var在ViewDIdLoad中可见,但在其他地方不可见

时间:2011-05-02 15:31:21

标签: iphone objective-c global-variables instance-variables protected

我正在编写一个iphone应用程序,并且存在问题;我可以在viewDidLoad方法中使用NSDate变量,但不能在其他地方使用!怎么做?怎么解决?

这是我的重要路线:

@interface GraphNavController : UINavigationController {

IBOutlet UIImage *image;
CGPoint gestureStartPoint;
IBOutlet UISegmentedControl *segmentedControl;

NSDateComponents *dateComponents;
NSCalendar *gregorian;

@public
NSDate *date;   

}

在实施代码中......

- (void)viewDidLoad {
[super viewDidLoad];

date = [[NSDate alloc] init];

//  Set the GMT @ 2
gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
[gregorian setTimeZone:[NSTimeZone timeZoneWithName:@"GMT+2"]];

dateComponents = [[NSDateComponents alloc] init];
[dateComponents setMonth:11];

[dateComponents setMonth:11];// tests ^^
date = [gregorian dateByAddingComponents:dateComponents toDate:date options:0];

NSLog(@"-->  Date : %@ - DComponents's : %d-%d-%d - gregorian : %@",date, [dateComponents day], [dateComponents month], [dateComponents year], [gregorian calendarIdentifier]); // is running here completely well

}

日期变量隐藏在此类的任何其他方法中......

我已经尝试将该变量放在@ public,@ private,@ package,......但没有任何内容正在运行。

该应用程序正在查杀而没有给出任何问题的描述!我只需要将日期的日志放在viewOnLoad中的其他任何地方,然后崩溃就会出现......

1 个答案:

答案 0 :(得分:1)

dateByAddingComponents:toDate:options:返回一个自动释放的对象。你应该保留它。如果您使用财产:

@property(retain,nonatomic) NSDate *date;

然后你可以这样做:

self.date = [gregorian dateByAddingComponents:dateComponents toDate:[NSDate date] options:0];