我需要一些從應用程序代理接收NSNotification
的UIViewController
。這就像一個計時器,但每個UIViewController
處理你的方式。我的問題是:當我與用戶界面交互時,UIViewController
未收到通知,導致出現問題。滾動內部UITableView時處理NSNotification
這是我在的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];
}
我處理每個UIViewController
這樣的通知:
-(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];
}
我應該怎麼做與UI交互,並在同一時間更新?也許NSNotification不會在用戶與UI交互時拋出。
你在主線程可以發佈您的通知? – RaffAl
@reecon是的,它在主線程中。這樣對嗎?! – marceloquinta