Search code examples
iosobjective-cuiviewcontrollernsnotificationcenternsnotification

Handle NSNotification while scrolling a inner UITableView


I need some UIViewControllers that receive a NSNotification from the app delegate. It's like a timer, but every UIViewController handle your way. My problem is: when I interact with user interface, the UIViewController doesn't receive the notification, causing problems.

Here is my code in AppDelegate:

-(void)updateCounter:(NSTimer *)theTimer{
    [[NSNotificationCenter defaultCenter] postNotificationName:TimeTickNotification object:nil];
}

//*called by some trigger in the app
-(void) startTimer{
    timer = [NSTimer
             scheduledTimerWithTimeInterval:0.5
             target:self
             selector:@selector(updateCounter:)
             userInfo:nil
             repeats:YES];
}

I am handling the notifications in each UIViewController like this:

-(void) updateGlobalTime:(NSNotification *) notification{
        totalTime = [NSNumber numberWithFloat:([ficha.tempoTotal floatValue] + STEP)];
}




-(void) viewWillAppear:(BOOL)animated {

    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(updateGlobalTime:)
                                                 name:TimeTickNotification
                                               object:nil];


}

What should I do to interact with UI and update it at same time? Maybe the NSNotification is not being thrown while user interacts with UI.


Solution

  • The solution was to use NSRunLoop, as following:

    NSRunLoop *runloop = [NSRunLoop currentRunLoop];
    timer = [NSTimer
                 scheduledTimerWithTimeInterval:0.5
                 target:self
                 selector:@selector(updateCounter:)
                 userInfo:nil
                 repeats:YES];
    [runloop addTimer:timer forMode:NSRunLoopCommonModes];
    [runloop addTimer:timer forMode:UITrackingRunLoopMode];