在一个视图中添加/移除视图的问题

3

我正在尝试在下面的视图中添加/删除一个视图(_countdown),添加视图没有问题。但是当我尝试删除它时,什么也不会发生。我的所有NSLog()都被调用,所以一切都应该正常工作。但是当到达-(void)removeAnimation(尝试使用动画移除时钟视图,使其在主视图中不可见)时,什么也不会发生。为什么?我真的看不出来为什么。我已经试图解决这个问题几个小时了,但我就是找不到任何方法让它工作...

@implementation MainViewController {
    ClockView *_clock;
    ClockView *_countdown;
}

viewDidLoad:

-(void)viewDidLoad{
timerAddClock = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(checkNotificationExist) userInfo:nil repeats:NO];
}

检查通知是否存在:

 -(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];
    }

移除动画

-(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];
                     }];

在 _countdown 分配、addSubview 和 removeFromSuperView 之前,添加 NSLog(@"%@", _countdown); 并发布结果。 - LombaX
如果 removeFromSuperView 被确实调用了,首先要想到的是:_countdown 没有指向你认为它指向的视图。它是否可能被创建多次,以至于值被覆盖而没有删除之前的对象?或者它在某个地方变成了 nil。如果你正在使用 ARC,也许它会过早地处理引用。检查 _countdown 初始化时的地址,然后再次检查它被移除时的地址,以验证它是相同的。 - Matt
分配后:AppName[1413:c07] <ClockView: 0x9df7170; frame = (0 -7; 232 86); layer = <CALayer: 0x23973b40>> - Christoffer
在执行 removeFromSuperView: 方法之前:<ClockView: 0x9df7170; frame = (112 -320; 232 86); animations = { position=<CABasicAnimation: 0x9d5d600>; }; layer = <CALayer: 0x23973b40>> - Christoffer
3个回答

1
一个可能的解释是,您可能正在添加更多的倒计时视图之一。因此,如果checkNotifications被调用多次(有一个间隔计时器,所以我认为这是可能的),并且在添加_countdown视图的行运行多次,那么如果您只删除其中一个视图,则不会看到任何内容,因为重复的视图将位于顶部。
只是一个想法。

1

首先,在您的- (void)viewDidLoad;方法中,应该调用[super viewDidLoad];。此外,您可能已经将_countdown视图添加为子视图,这可能会导致问题?

您还可以再次检查所有NSTimer,并确保它们被正确调用。您的addAnimation计时器对我来说有点可疑。

您可以尝试仅隐藏视图而不是从父视图中删除它。那肯定会起作用。

希望这有所帮助!


成功了。现在感觉好傻。在多个地方调用了添加视图的计时器。非常感谢! - Christoffer

0
根据您的最后一条评论,当您调用removeFromSuperview时似乎有一个动画正在进行中,因此视图没有被移除。
我认为问题在于您的checkNotificationExists方法:您创建了一个动画,但是在完成块之外启动了计时器。尝试将其移动到完成块内部。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接