datePicker 多次选择后崩溃

发布于 2024-12-07 15:02:12 字数 2377 浏览 0 评论 0原文

我玩得很开心。我有一个 datePicker 设置开始和结束日期,就像苹果日历一样。每当我多次单击 datePicker 时,我都会崩溃,没有任何消息。我猜我内存泄漏但找不到它。请帮忙...因为我需要通过多种方法访问这些内容,所以我将 UIDatePicker 和 NSDateFormatter 制作成了 ivars。当按下按钮时,BOOL 被设置,并且所有内容都被设置为接受 IB 中的多次触摸。

如果 datePicker 仅单击一次或有时两次,则工作正常。

.h

IBOutlet UIDatePicker *datePicker;    
NSDateFormatter *dateFormatter;    
NSDateFormatter *timeFormatter;   

@property (nonatomic, retain) UIDatePicker *datePicker;    
@property (nonatomic, retain) NSDateFormatter *dateFormatter;
@property (nonatomic, retain) NSDateFormatter *timeFormatter;

.m

@synthesize datePicker;
@synthesize dateFormatter, timeFormatter;

viewDidLoad:

NSDate *now = [[NSDate alloc] init];

[datePicker setDate:now animated:NO];
//known bug - hopefully fixed in 4.3
//[datePicker setMinimumDate:now];
[now release];

self.dateFormatter = [[NSDateFormatter alloc] init];
[self.dateFormatter setDateStyle:NSDateFormatterMediumStyle];
[self.dateFormatter setTimeStyle:NSDateFormatterNoStyle];

formattedDateStringDate = [self.dateFormatter stringFromDate:datePicker.date];
NSLog(@"initdate %@", formattedDateStringDate);

self.timeFormatter = [[NSDateFormatter alloc] init];
[self.timeFormatter setDateStyle:NSDateFormatterNoStyle];
[self.timeFormatter setTimeStyle:NSDateFormatterShortStyle];

formattedDateStringTime = [self.timeFormatter stringFromDate:datePicker.date];
NSLog(@"initTime %@", formattedDateStringTime);

valueDidChange:

- (IBAction)changeDateInLabel:(id)sender
{
    formattedDateStringDate = [dateFormatter stringFromDate:datePicker.date];
    //NSLog(@"date %@", formattedDateStringDate);

    formattedDateStringTime = [timeFormatter stringFromDate:datePicker.date];
    //NSLog(@"time %@", formattedDateStringTime);

    if (isStartDateSet)
    {
        startDateLabel.text = formattedDateStringDate;
        startTimeLabel.text = formattedDateStringTime;
        newDate = formattedDateStringDate;
        newStartTime = formattedDateStringTime;
    }

    if (isEndDateSet)
    {
        endTimeLabel.text = formattedDateStringTime;
        newEndTime = formattedDateStringTime;
    }
}   

viewDidUnload 中将所有三个设置为 nil所有三个都在 dealloc 中发布。

Im having a heck of a time. I have a datePicker setting start and end dates, like the apple calendar. I am crashing with no messages whenever I click the datePicker multiple times. I'm guessing Im leaking memory but cannot find it. please help... ecause I need to access these in multiple methods, I made my UIDatePicker and NSDateFormatter into ivars. the BOOL's are set when buttons are pressed and everything is set up to accept multiple touches in IB.

works fine if the datePicker is only clicked once or sometimes twice.

.h

IBOutlet UIDatePicker *datePicker;    
NSDateFormatter *dateFormatter;    
NSDateFormatter *timeFormatter;   

@property (nonatomic, retain) UIDatePicker *datePicker;    
@property (nonatomic, retain) NSDateFormatter *dateFormatter;
@property (nonatomic, retain) NSDateFormatter *timeFormatter;

.m

@synthesize datePicker;
@synthesize dateFormatter, timeFormatter;

viewDidLoad:

NSDate *now = [[NSDate alloc] init];

[datePicker setDate:now animated:NO];
//known bug - hopefully fixed in 4.3
//[datePicker setMinimumDate:now];
[now release];

self.dateFormatter = [[NSDateFormatter alloc] init];
[self.dateFormatter setDateStyle:NSDateFormatterMediumStyle];
[self.dateFormatter setTimeStyle:NSDateFormatterNoStyle];

formattedDateStringDate = [self.dateFormatter stringFromDate:datePicker.date];
NSLog(@"initdate %@", formattedDateStringDate);

self.timeFormatter = [[NSDateFormatter alloc] init];
[self.timeFormatter setDateStyle:NSDateFormatterNoStyle];
[self.timeFormatter setTimeStyle:NSDateFormatterShortStyle];

formattedDateStringTime = [self.timeFormatter stringFromDate:datePicker.date];
NSLog(@"initTime %@", formattedDateStringTime);

valueDidChange:

- (IBAction)changeDateInLabel:(id)sender
{
    formattedDateStringDate = [dateFormatter stringFromDate:datePicker.date];
    //NSLog(@"date %@", formattedDateStringDate);

    formattedDateStringTime = [timeFormatter stringFromDate:datePicker.date];
    //NSLog(@"time %@", formattedDateStringTime);

    if (isStartDateSet)
    {
        startDateLabel.text = formattedDateStringDate;
        startTimeLabel.text = formattedDateStringTime;
        newDate = formattedDateStringDate;
        newStartTime = formattedDateStringTime;
    }

    if (isEndDateSet)
    {
        endTimeLabel.text = formattedDateStringTime;
        newEndTime = formattedDateStringTime;
    }
}   

all three set to nil in viewDidUnload and all three released in dealloc.

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

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

发布评论

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

评论(1

薄暮涼年 2024-12-14 15:02:12

在进一步调查之前,您需要更改一些内容:

  • self.dateFormatter = [[NSDateFormatter alloc] init];

此后,dateFormatter 的保留计数为 2,其中 alloc 为 1,属性“retain”为 1 。将其更改为 dateFormatter = [[NSDateFormatter alloc] init]; 对于 timeFormatter 和可能的 datePicker 也是如此。

  • 将成员设置为零后,您将无法再释放它。反过来做。

You need to change some things before further investigation:

  • self.dateFormatter = [[NSDateFormatter alloc] init];

After this the dateFormatter's retain count is 2, 1 for the alloc and 1 for the property 'retain'. Change it to just dateFormatter = [[NSDateFormatter alloc] init]; Same for the timeFormatter and probably the datePicker too.

  • After setting a member to nil, you cannot release it anymore. Do it the other way round.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文