帮助更改textLabel的NSTimer.代码包括在内

Bas*_*App 2 iphone xcode

想要将我的NSTimer代码更改为使用applicationSignificantTimeChange

这就是我现在所拥有的.

基本上我想更改计时器,而不是每5秒更换一次,例如我希望这些textLabels每天晚上12点更改,有人帮我解决?

//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]; 
Run Code Online (Sandbox Code Playgroud)

Rob*_*und 13

您可以做两件事来响应applicationSignificantTimeChange:

(1)只需实现applicationSignificantTimeChange:在你的应用委托中,如果你有一个应该更新的视图控制器的连接.

- (void)applicationSignificantTimeChange:(UIApplication *)application {
  yourViewController.textLabel.text = @"random text";
}
Run Code Online (Sandbox Code Playgroud)

(2)在应该获得更新的视图控制器中订阅UIApplicationSignificantTimeChangeNotification通知.您也许可以将该代码放在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";
}
Run Code Online (Sandbox Code Playgroud)

你还需要打电话

[[NSNotificationCenter defaultCenter] removeObserver:self];
Run Code Online (Sandbox Code Playgroud)

不再需要视图控制器的地方.