2012-11-16 50 views
3

我想要添加/刪除視圖(_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]; 
        }]; 
+0

添加NSLog(@「%@」,_countdown);在_countdown分配後,添加子視圖和removefromsuperview之前併發布結果。 – LombaX

+0

如果removeFromSuperView是絕對調用的,首先會想到一件事:_countdown並不指向您認爲它的視圖。是否有可能創建了多次,使得在不刪除前一個對象的情況下覆蓋該值?或者,也許它在這個過程中一直處於零。如果您使用的是ARC,可能是過早地處理該參考。在初始化時檢查_countdown的地址,然後在刪除時再次檢查地址是否相同。 – Matt

+0

分配後:AppName [1413:c07] > – Christoffer

回答

1

首先,您應該在- (void)viewDidLoad;方法中調用[super viewDidLoad];。另外,您可能已將_countdown視圖添加爲某個子視圖,這可能會導致問題?

您可能還想要再次檢查您的所有NSTimer,並確保它們被正確調用。你的addAnimation計時器似乎對我很可疑。

您可以嘗試隱藏視圖,而不是將其從超級視圖中移除。這肯定會起作用。

希望有助於!

+0

它的工作。現在感覺很愚蠢。調用定時器以在多個位置添加視圖。非常感謝! – Christoffer

+0

沒問題!樂意效勞! – msgambel

1

一個可能的解釋是,您可能會添加更多的_countdown視圖之一。所以如果多次調用checkNotifications(有一個間隔計時器,所以我認爲這是可能的),並且添加_countdown視圖的行不止一次運行,那麼如果你只刪除了你贏得的視圖,沒有看到任何東西,因爲重複的觀點是最重要的。

只是一個想法。

0

根據您最近的評論,當您調用removeFromSuperview時,似乎有一個動畫正在進行。所以,這個觀點並沒有被刪除。

我認爲問題出在您的checkNotificationExists方法中:您創建一個動畫,但在完成塊之外啓動計時器。嘗試在完成塊內移動它

相關問題