我将ID设置为nil后,IOS Object在计时器上重新生成

时间:2013-06-30 15:48:20

标签: ios6 timer null

DemoViewController负责向用户显示教程。包含动画和计时器,以便在用户忽略时重复手势演示。是从DataViewController实例化的。是nil-ed,但后来在内部计时器上重生了。我需要它完全消失,以便在用户返回第一页时不再创建它。

dataViewController.h

#import "DemoViewController.h"
@property (strong,nonatomic) DemoViewController *demoController;

dataViewController.h

-(void) viewWillAppear:(BOOL)animated {
    // demoPageNumber is 0
    if ((self.demoController== nil) && ([_pageNumber isEqualToNumber:demoPageNumber])){
       self.demoController = [[DemoViewController alloc] initWithView:self.view];
    }
}

-(void) viewWillDisappear:(BOOL)animated{
    [self.demoController free]; // invalidate timer, nil all internal objects
    self.demoController=nil; // should free object
}

DemoViewController.m

-(void) free{
   [animationRespawnTimer invalidate];
   animationRespawnTimer=nil;
}

-(void) respawnDemoWithSelector:(SEL)selector{
    NSLog(@"Timer fired %@", self);
    [self resetTimer];
    animationRespawnTimer = [NSTimer scheduledTimerWithTimeInterval:10
                                                        target:self
                                                      selector:selector
                                                      userInfo:nil
                                                       repeats:NO];
}

-(void) showScrollDemo{


    NSLog(@"showScrollDemo fired");
    [self stopPreviousAnimations];
    scrollHandView.frame = CGRectMake(315.0, 700.0, 100, 100);
    scrollHandView.hidden=NO;
    scrollHandView.alpha=1;

    [UIView animateWithDuration:3.0
                          delay: 0.0
                        options: (UIViewAnimationOptionCurveEaseOut |
                                  UIViewAnimationOptionRepeat )
                     animations:^{

                         [UIView setAnimationRepeatCount:3];
                         scrollHandView.frame = CGRectMake(315.0, 300.0, 100, 100);

                     }
                     completion:^(BOOL finished){

                         [UIView animateWithDuration:1.0 delay:0 
                        options:UIViewAnimationOptionCurveEaseOut
                                      animations:^{
                                          scrollHandView.alpha=0;

                          }
                            completion:^(BOOL finished){
                            scrollHandView.hidden=YES;

                    [self respawnDemoWithSelector: @selector(showScrollDemo)];

                          }
                      ];

                 }
 ];

}

当加载页面时,如果这是第一页,则实例化demoController,并在清理后退出页面nil-ed(自定义方法)。根据我的理解,这应该删除demoController对象及其所有内容,包括计时器。调试区域显示正是这样!直到在新页面上,demoController计时器神秘地从以前的对象ID重新生成。

17:59:14.041 viewWillAppear begin (self.demoController null)
18:00:05.346 viewWillAppear, <DemoViewController: 0x7580310> //demoController created
18:00:15.786 in the demoController method the "showScrollDemo" is fired
18:00:19.834 viewWillAppear end <DemoViewController: 0x7580310>

页面已加载,演示正常。现在我正在翻页。触发了viewWillDisappear事件。

18:01:17.966 viewWillDisappear begin, send "free" message to demoController 
18:01:17.966 "free" was performed from <DemoViewController: 0x7580310>
18:01:34.059 viewWillDisappear end (self.demoController null)

因此,“self.demoController”为null。然后demoController以先前的ID

重新生成自己
18:02:36.514 Timer fired <DemoViewController: 0x7580310>

为什么呢?计时器无法重生,设置为重复:否。

1 个答案:

答案 0 :(得分:1)

我假设它是动画的完成块,它调用respawnDemoWithSelector并创建一个新的计时器。

根据这个答案:https://stackoverflow.com/a/9676508/1187415,你可以停止所有的运行 动画

[self.view.layer removeAllAnimations];

或者,您可以将布尔属性done添加到已设置的DemoViewController 到YES方法中的free,并检查动画的完成块:

if (!self.done)
    [self respawnDemoWithSelector: @selector(showScrollDemo)];

更新:动画块捕获对self的强引用,从而防止 被解除分配的对象。该“保留周期”问题的标准解决方案 (假设您使用ARC)是对自己使用弱引用。这看起来像这样:

__weak typeof(self) weakSelf = self;
[UIView animateWithDuration:3.0
                      delay: 0.0
                    options: (UIViewAnimationOptionCurveEaseOut |
                              UIViewAnimationOptionRepeat )
                 animations:^{

                     [UIView setAnimationRepeatCount:3];
                     weakSelf.scrollHandView.frame = CGRectMake(315.0, 300.0, 100, 100);

                 }
                 completion:^(BOOL finished){

                     [UIView animateWithDuration:1.0 delay:0
                                         options:UIViewAnimationOptionCurveEaseOut
                                      animations:^{
                                          weakSelf.scrollHandView.alpha=0;

                                      }
                                      completion:^(BOOL finished){
                                          weakSelf.scrollHandView.hidden=YES;
                                          [weakSelf respawnDemoWithSelector: @selector(showScrollDemo)];

                                      }
                      ];

                 }
 ];

weakSelf 持有对DemoViewController的强引用,并设置为nil 如果它指向的对象被释放,则自动进行。在这种情况下,发送到块内weakSelf的所有消息都不会发生任何事情。