显示 UIAlertController 后,我启动了一个调用 timerLabel 的计时器。这应该只更新 UIAlertControllers 动作标题。NSLog 显示标题实际上从 25 变为 1。但是 UIAlertController 上的实际标题继续显示“25”。
我是否必须调用任何东西来更新标题才能显示?当我在 8.3 上运行确切的代码时,它可以工作,但是那是 beta 版,可能意味着它在实际发布时不起作用,所以我也需要它在非 beta 8.2 上工作。感谢任何建议。
NSTimer *buttonTimer;
UIAlertController *touchAdverts;
int timeStart;
-(void)viewDidAppear:(BOOL)animated{
[super viewDidAppear:animated];
timeStart = 25;
//inform user they need to touch adverts
touchAdverts = [UIAlertController alertControllerWithTitle:@"IMPORTANT" message:@"text to show user" preferredStyle:UIAlertControllerStyleAlert];
[touchAdverts addAction:[UIAlertAction actionWithTitle:[NSString stringWithFormat:@"%i",timeStart] style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
//invalidate timer when button pressed
[buttonTimer invalidate];
[[NSUserDefaults standardUserDefaults]setBool:YES forKey:@"hasHadTimedAlert"];
[[NSUserDefaults standardUserDefaults]synchronize];
}]];
//disables the action button from being interated with
[touchAdverts.actions[0]setEnabled:NO];
if ( ![[NSUserDefaults standardUserDefaults]boolForKey:@"hasHadTimedAlert"]) {
[self.view.window.rootViewController presentViewController:touchAdverts animated:YES completion:^{
//when alert is shown start timer to update the title of the alerts action
buttonTimer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(timerLabel) userInfo:nil repeats:YES];
}];
}
}
-(void)timerLabel{
if (timeStart >0) {
//countdown from timeStart
timeStart -= 1;
//update the action button title each second to new timeStart value
[touchAdverts.actions[0] setTitle:[NSString stringWithFormat:@"%i",timeStart]];
NSLog(@"%@",[touchAdverts.actions[0] title]);
}else{
//When value of timeStart reaches 0 enable the action button and change title
[touchAdverts.actions[0] setEnabled:YES];
[touchAdverts.actions[0] setTitle:@"I have read & understood the above"];
}
}