Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Problems with adding/removing a view within a view

I'm trying to get add/remove a view (_countdown) to a view below, no problems with adding the view. But when I'm trying to remove it, nothing happens. All of my NSLog() are getting called, so everything should work. But when it gets to -(void)removeAnimation (trying to remove the clockview with an animation, so it's not visible in the mainview anymore), nothing happens. Why? I can't really see why. I've been trying to figure this out for a couple of hours now but I just can't find any way of getting it to work...

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

checkTime

-(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];
                     }];
like image 650
Christoffer Avatar asked Jul 21 '26 11:07

Christoffer


2 Answers

First, you should call [super viewDidLoad]; in your - (void)viewDidLoad; method. Also, you may have added the _countdown view as a subview somewhere, which may be causing issues?

You may also want to check all your NSTimer's again, and make sure that they are being called correctly. Your addAnimation timer seems suspicious to me.

You could try just hiding the view instead of removing it from the superview. That would work for sure.

Hope that Helps!

like image 134
msgambel Avatar answered Jul 24 '26 03:07

msgambel


One possible explanation is that you might be adding more of one of that _countdown views. So if checkNotifications is called several times (there is a timer with an interval, so I assume that is possible) and the line where to add the _countdown view is run more than once, then if you only remove on of the views you won't see anything because the repeated view would be on top.

Just an idea.

like image 39
LocoMike Avatar answered Jul 24 '26 02:07

LocoMike