datePicker 多次选择后崩溃
我玩得很开心。我有一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在进一步调查之前,您需要更改一些内容:
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.