// 日曆視圖彈出
- (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。