倒计时NSTimer在一个UIAlertController的标题中获得一个空值,而不是几秒钟的时间

我试图在UIAlertController的标题中显示一个倒计时。 我想要“你的会议将在X秒内过期”。 我的思考过程是创build一个NSTimer,并将时间存储在NSString stringWithFormat中,并将该string作为警报控制器的标题。 这是我的countDown方法:

@interface ViewController () { NSString *seconds; int mainInt; NSTimer *timer; } - (void)countDown{ mainInt = 20; mainInt -= 1; seconds = [NSString stringWithFormat:@"%i", mainInt]; if (mainInt == 0) { [timer invalidate]; } } 

这是由IBAction触发的UIAlertController。 当触发控制器获取模态和标题说“空秒”等待几秒钟,并尝试再次触发注销button,你会得到一个标题说:“19秒”。

 - (IBAction)logout:(id)sender { timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(countDown) userInfo:nil repeats:YES]; NSString *logOutString = [NSString stringWithFormat:@"%@ seconds.", seconds]; UIAlertController *alertController = [UIAlertController alertControllerWithTitle:nil message:logOutString preferredStyle:UIAlertControllerStyleActionSheet]; UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"Cancel", @"Cancel action") style:UIAlertActionStyleCancel handler:^(UIAlertAction *action){ NSLog(@"Cancel Log out"); }]; UIAlertAction *okAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"log Out", @"Ok action") style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action){ NSLog(@"Loged out"); //Log out code goes here //When time is up, log out automatically }]; [alertController addAction:okAction]; [alertController addAction:cancelAction]; [alertController setModalPresentationStyle:UIModalPresentationPopover]; [self presentViewController:alertController animated:YES completion:nil]; } 

你的第一个问题是你没有初始化任何东西,所以它最初将是零。 这就是为什么你的警报说“空秒”。 在随后的警报显示中,秒数将由您的countDown方法设置。

第二个问题是,实际上并没有更新警报的message属性,所以当警报最初显示时,您只能看到seconds的值。

最后,你将mainInt设置为20,然后每次定时器触发时减1,所以它永远不会达到0,它将是20,19,20,19,20,19 …

下面的代码在IBAction方法中初始化mainInt ,所以每次层次mainInt时它不会重置为20。 它还更新了警报的message属性,并在定时器到达0时closures警报(实际上它在1秒后执行,因为我认为看到2..1 … 0 …更好,但是可以更改那)

 @interface ViewController () { int mainInt; NSTimer *timer; UIAlertController *alertController; } - (NSString *)countDownString { return [NSString stringWithFormat:@"%i seconds", mainInt]; } - (void)countDown{ mainInt -= 1; if (mainInt < 0) { [timer invalidate]; [alertController dismissViewControllerAnimated:YES completion:^{ // Whatever you need to do to complete the logout }] } else { alertController.message=[self countDownString]; } } - (IBAction)logout:(id)sender { timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(countDown) userInfo:nil repeats:YES]; mainInt=20; alertController = [UIAlertController alertControllerWithTitle:nil message:[self countDownString] preferredStyle:UIAlertControllerStyleActionSheet]; UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"Cancel", @"Cancel action") style:UIAlertActionStyleCancel handler:^(UIAlertAction *action){ [timer invalidate]; NSLog(@"Cancel Log out"); }]; UIAlertAction *okAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"log Out", @"Ok action") style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action){ [timer invalidate]; NSLog(@"Loged out"); //Log out code goes here //When time is up, log out automatically }]; [alertController addAction:okAction]; [alertController addAction:cancelAction]; [alertController setModalPresentationStyle:UIModalPresentationPopover]; [self presentViewController:alertController animated:YES completion:nil]; }