1

显示 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"];
}

}
4

1 回答 1

2

目前,一旦显示视图,您就无法修改警报控制器视图的可见标题(尽管从您的实验中听起来好像这将在 iOS 8.3 最终版本中开始工作)。但是你并不真的需要,因为用你自己的视图来编写你自己的视图控制器很容易,它看起来和行为就像一个警报视图控制器的视图。当您这样做时,您可以完全控制界面。因此,如果您希望它在 iOS 8.2 及之前的版本中工作,我建议您这样做。

于 2015-04-07T15:57:48.810 回答