// 日历视图弹出
- (void)slideInCalView:(UITextField *)textField
{
if (_calView==nil) {
_calView = [[XYCalendarView alloc] initWithFrame:self.view.bounds andSelectedDate:[NSDate date] bottomBar:YES];
// _calView = [[XYCalendarView alloc] initComplexGridWithFrame:self.view.bounds andSelectedDate:[NSDate date] bottomBar:YES];
_calView.delegate = self;
}
_calView.frame=self.view.bounds;
[self.view addSubview:_calView];
[UIView animateWithDuration:0.4 delay:0 options:UIViewAnimationOptionCurveLinear animations:^{
_calView.frame = CGRectMake(0, 0, _calView.frame.size.width, _calView.frame.size.height);
} completion:^(BOOL finished) {
}];
}
日历视图关闭 使用delegate method
- (void)slideOutCalendarView:(XYCalendarDay *)selectedTileDay
{
[UIView animateWithDuration:.5 delay:0 options:UIViewAnimationOptionCurveLinear animations:^{
_calView.frame = CGRectMake(0, _calView.frame.size.height, _calView.frame.size.width, _calView.frame.size.height);
} completion:^(BOOL finished) {
[_calView removeFromSuperview];
}];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyyMMdd"];
NSString *strDate = [dateFormatter stringFromDate:selectedTileDay.calDate];
// GTMLoggerDebug(@"selectedTileDate is %@", strDate);
_editingTextField.text=strDate;
_calView=nil;
}
[UIView animateWithDuration:.5 delay:0 options:UIViewAnimationOptionCurveLinear animations:^{
_calView.frame = CGRectMake(0, _calView.frame.size.height, _calView.frame.size.width, _calView.frame.size.height);
} completion:^(BOOL finished) {
[_calView removeFromSuperview];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyyMMdd"];
NSString *strDate = [dateFormatter stringFromDate:selectedTileDay.calDate];
// GTMLoggerDebug(@"selectedTileDate is %@", strDate);
_editingTextField.text=strDate;
_calView=nil;
}];
奇怪的一点是,释放内存的代码是removeFromSuperview 而不是nil.
不对,还是nil释放内存,只是没有remove掉,表示还有对象在使用它,所以nil时不释放内存。
这里是没有remove掉,那么nil也不会成功。
subview设置为nil后,却没有removefromsuperview,那么占有的内存还在。这就导致来内存泄漏。
非常隐蔽的内存泄漏。
正确的方式是:
先remove后nil才是正确的是否内存的方式。如果还向对象继续留在内存里,下次调用不用定义,这在 主视图退出时nil。