gpt4 book ai didi

ios - 滚动内部 UITableView 时处理 NSNotification

转载 作者:行者123 更新时间:2023-11-28 22:02:41 24 4
gpt4 key购买 nike

我需要一些 UIViewController 来接收来自应用委托(delegate)的 NSNotification。它就像一个计时器,但每个 UIViewController 都按照您的方式处理。我的问题是:当我与用户界面交互时,UIViewController 没有收到通知,导致出现问题。

这是我在 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 交互并同时更新它?可能在用户与 UI 交互时没有抛出 NSNotification。

最佳答案

您需要确保在主线程上更新任何 UI。如果您想更新 UI 以在标签或其他内容中包含新的 totalTime,请确保 setText: 函数正在主线程上运行。您可以使用 GCD 完成此操作,如下所示:

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

// Update label on main thread
dispatch_async(dispatch_get_main_queue(), ^{

[label setText:totalTime];
});
}

关于ios - 滚动内部 UITableView 时处理 NSNotification,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24739810/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com