为什么UIAlertController的标题没有更新?

时间:2015-04-07 15:40:21

标签: ios8 nstimer uialertcontroller uialertaction

显示UIAlertController后,我启动一个调用timerLabel的计时器。这应该只更新UIAlertControllers动作标题。 NSLog显示标题实际上从25变为1.然而,UIAlertController上的实际标题继续显示" 25"。

我是否需要调用任何内容来更新标题以便显示?当我在8.3上运行确切的代码时,它可以运行测试版,可能意味着它在实际发布时不会工作,所以我也需要它来处理非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"];
}

}

1 个答案:

答案 0 :(得分:2)

目前,一旦显示视图,您就无法修改警报控制器的可见标题(尽管从您的实验中发出的声音就像在iOS 8.3中开始工作一样最后)。但是你并不是真的需要,因为很容易用自己的视图编写自己的视图控制器,其外观和行为类似于警报视图控制器的视图。执行此操作时,您可以完全控制界面。因此,如果您希望这在iOS 8.2及更早版本中运行,那么我建议您这样做。