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

发布于 2024-11-04 18:31:00 字数 1265 浏览 1 评论 0原文

我正在编写一个 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 以外的任何地方,就会出现崩溃......

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], [gregorian 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...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

淡墨 2024-11-11 18:31:00

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

@property(retain,nonatomic) NSDate *date;

那么您可以这样做:

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

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];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文