我想要添加/刪除視圖(_countdown)到下面的視圖,添加視圖沒有問題。但是,當我試圖將其刪除時,沒有任何反應。我所有的NSLog()都被調用,所以一切都應該工作。但是當它到達 - (void)removeAnimation(試圖用動畫去除clockview,所以它在主視圖中不再可見),沒有任何反應。爲什麼?我真的不明白爲什麼。我一直在想我們這出了幾個小時,但我只是找不到得到它的工作的任何方式......在視圖中添加/刪除視圖的問題
@implementation MainViewController {
ClockView *_clock;
ClockView *_countdown;
}
viewDidLoad中:
-(void)viewDidLoad{
timerAddClock = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(checkNotificationExist) userInfo:nil repeats:NO];
}
checkNotificationExist:
-(void)checkNotificationExist {
NSUserDefaults *userDef = [NSUserDefaults standardUserDefaults];
if ([userDef boolForKey:@"NotificationExist"]) {
NSDate *notificationDate = [[NSUserDefaults standardUserDefaults] valueForKey:@"NotificationDate"];
NSDate *now = [NSDate date];
NSTimeInterval interval = [notificationDate timeIntervalSinceDate:now];
if (interval > 0) {
if (_clock == nil) {
_countdown = [[ClockView alloc] initWithCountdownToTime:[NSDate dateWithTimeIntervalSinceNow:interval]];
[self.view addSubview:_countdown];
[UIView animateWithDuration:0.5f
animations:^{
[_countdown setFrame:CGRectMake((self.view.frame.size.width - _countdown.frame.size.width)/2, (self.view.frame.size.height), _countdown.frame.size.width, _countdown.frame.size.height)];
}];
}
self.timeIntervalTimer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(checkTime) userInfo:nil repeats:YES];
}
}
檢查時間
-(void)checkTime {
NSLog(@"running");
[timerAddClock invalidate];
self.timerAddClock = nil;
NSDate *notificationDate = [[NSUserDefaults standardUserDefaults] valueForKey:@"NotificationDate"];
NSDate *now = [NSDate date];
NSTimeInterval interval = [notificationDate timeIntervalSinceDate:now];
if (interval < 1) {
NSUserDefaults *userDef = [NSUserDefaults standardUserDefaults];
[userDef setBool:NO forKey:@"NotificationExist"];
[[NSUserDefaults standardUserDefaults] removeObjectForKey:@"NotificationDate"];
self.addAnimation = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(removeAnimation) userInfo:nil repeats:YES];
}
removeAnimation
-(void)removeAnimation {
NSLog(@"Removing animation...");
[UIView animateWithDuration:0.5f
animations:^{
[_countdown setFrame:CGRectMake((self.view.frame.size.width - _countdown.frame.size.width)/2, (self.view.frame.size.height - 800), _countdown.frame.size.width, _countdown.frame.size.height)];
} completion:^(BOOL finished) {
[_countdown removeFromSuperView];
}];
添加NSLog(@「%@」,_countdown);在_countdown分配後,添加子視圖和removefromsuperview之前併發布結果。 – LombaX
如果removeFromSuperView是絕對調用的,首先會想到一件事:_countdown並不指向您認爲它的視圖。是否有可能創建了多次,使得在不刪除前一個對象的情況下覆蓋該值?或者,也許它在這個過程中一直處於零。如果您使用的是ARC,可能是過早地處理該參考。在初始化時檢查_countdown的地址,然後在刪除時再次檢查地址是否相同。 – Matt
分配後:AppName [1413:c07]> –
Christoffer