Var visible in the ViewDIdLoad but not somewhere else
I'm programming one iphone app, and there is a problem; I can use a NSDate variable in the viewDidLoad method but not somewhere else! How can it be made? how can it be resolved?
here's my important lines:
@interface GraphNavController : UINavigationController {
IBOutlet UIImage *image;
CGPoint gestureStartPoint;
IBOutlet UISegmentedControl *segmentedControl;
NSDateComponents *dateComponents;
NSCalendar *gregorian;
@public
NSDate *date;
}
And in the implementation code...
- (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], [gregoria开发者_StackOverflow中文版n calendarIdentifier]); // is running here completely well
}
And the date variable is hidden in any other method of this class...
I already try to put that variable in @public, @private, @package, ... but nothing is running.
The app is killing without give any description of the problem! I just have to put a Log of the date anywhere else than in the viewOnLoad and the crash appears...
dateByAddingComponents:toDate:options:
returns an autoreleased object. You should retain it. If you use a property:
@property(retain,nonatomic) NSDate *date;
then you can do this:
self.date = [gregorian dateByAddingComponents:dateComponents toDate:[NSDate date] options:0];
精彩评论