问题描述
想要将我的NSTimer代码更改为使用applicationSignificantTimeChange
want to change my NSTimer code to to use applicationSignificantTimeChange
这就是我现在所拥有的。
This is what I have right now.
基本上我想更改计时器,而不是每5秒更换一次,例如我希望这些textLabels每天晚上12点更改,有人帮我解决?
Basically I want to change the timer, instead of changing every 5 seconds for example I want these textLabels to change at 12am every night anyone help me out?
//h file
NSTimer *timer;
IBOutlet UILabel *textLabel;
//m file
- (void)onTimer {
static int i = 0;
if ( i == 0 ) {
textLabel.text = @"iphone app";
}
else if ( i == 1 ) {
textLabel.text = @" Great App!";
}
else if ( i == 2 ) {
textLabel.text = @" WOW!";
}
else {
textLabel.text = @" great application again!!";
i = -1;
}
i++;
}
timer =[NSTimer scheduledTimerWithTimeInterval:5.0 target:self selector:@selector(onTimer) userInfo:nil repeats:YES];
推荐答案
你可以做两件事来回应applicationSignificantTimeChange:
There are two things you can do to respond to a applicationSignificantTimeChange:
(1)如果你有一个应该更新的视图控制器的连接,只需在app delegate中实现applicationSignificantTimeChange:
(1) Just implement applicationSignificantTimeChange: in your app delegate if you have a connection to the view controller that should be updated.
- (void)applicationSignificantTimeChange:(UIApplication *)application {
yourViewController.textLabel.text = @"random text";
}
(2)在视图控制器中订阅应该获得的UIApplicationSignificantTimeChangeNotification通知更新。您可以将该代码放入viewDidLoad
(2) Subscribe to the UIApplicationSignificantTimeChangeNotification notification in the view controller that should get the update. You could perhaps put that code in viewDidLoad
- (void)viewDidLoad {
[super viewDidLoad];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(onSignificantTimeChange:)
name:UIApplicationSignificantTimeChangeNotification
object:nil];
}
- (void)onSignificantTimeChange:(NSNotification *)notification {
self.textLabel.text = @"random text";
}
您还需要致电
[[NSNotificationCenter defaultCenter] removeObserver:self];
当您的视图控制器不再需要时。
somewhere when your view controller is not longer needed.
这篇关于帮助更改textLabel的NSTimer。代码包括在内的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!