NSTimer countdown in UIAlertController header getting zero instead of time in seconds

I am trying to display the countdown in the UIAlertController header. I want to have something like "Your session will expire in X seconds." My thought process was to create an NSTimer and save the time in an NSString stringWithFormat, and that line would be the title of the control controller. Here is my countDown method:

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

and this is a UIAlertController initiated by IBAction. When triggered, the controller is viewed modally, and the heading says that β€œzero seconds” wait a few more seconds, and try again to call out the logout button, and you will get the name β€œ19 seconds”.

 - (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]; } 
+1
source share
1 answer

Your first problem is that you are not initializing seconds anything, so it will initially be zero. This is why your warning says "null seconds". The next time the warning is displayed, the seconds will be set by your countDown method.

The second problem is that you are not actually updating the message property of your warning, so you just see the value of seconds when the warning was initially displayed.

Finally, you set mainInt to 20, and then subtract 1 every time the timer fires, so it will never reach 0, it will be 20,19,20,19,20,19 ...

The code below initializes mainInt in the IBAction method, so it does not reset to 20 every time ticks are ticked. It also updates the warning message property and rejects the warning when the timer reaches 0 (it actually does it 1 second after I think it's better to see 2..1 ... 0 ... logout, but you can change it)

 @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]; } 
+4
source

Source: https://habr.com/ru/post/1247465/


All Articles