Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/94.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios UIAlertController标题中的倒计时NSTimer获取空值,而不是以秒为单位的时间_Ios_Objective C_Cocoa Touch - Fatal编程技术网

Ios UIAlertController标题中的倒计时NSTimer获取空值,而不是以秒为单位的时间

Ios UIAlertController标题中的倒计时NSTimer获取空值,而不是以秒为单位的时间,ios,objective-c,cocoa-touch,Ios,Objective C,Cocoa Touch,我试图在UIAlertController的标题中显示倒计时。我想要类似“您的会话将在X秒后过期”的内容。我的想法是创建一个NSTimer并将时间存储在NSString stringWithFormat中,并将该字符串作为警报控制器的标题。 这是我的倒计时方法: @interface ViewController () { NSString *seconds; int mainInt; NSTimer *timer; } - (void)countDown{ mai

我试图在UIAlertController的标题中显示倒计时。我想要类似“您的会话将在X秒后过期”的内容。我的想法是创建一个NSTimer并将时间存储在NSString stringWithFormat中,并将该字符串作为警报控制器的标题。 这是我的倒计时方法:

@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。当被触发时,控制器会以模式查看,标题为“空秒”,再等待几秒钟,然后再次尝试触发注销按钮,您会看到标题为“19秒”


您的第一个问题是您没有初始化
seconds
为任何值,因此它最初将为零。这就是您的警报显示“空秒”的原因。在随后的警报显示中,秒数将由
倒计时方法设置

第二个问题是,您实际上没有更新警报的
消息
属性,因此您只看到警报最初显示时
秒的值

最后,将
mainit
设置为20,然后每次定时器启动时减去1,这样它就永远不会达到0,它将是20,19,20,19,20,19

下面的代码在iAction方法中初始化了
mainit
,因此不会在层每次滴答声时重置为20。它还会更新警报的
message
属性,并在计时器达到0时解除警报(实际上它会在1秒钟后解除警报,因为我认为最好看到2..1…0…注销,但您可以更改它)

@界面视图控制器(){
内部维护;
n定时器*定时器;
UIAlertController*alertController;
}
-(NSString*)倒计时字符串{
返回[NSString stringWithFormat:@“%i秒”,mainit];
}
-(无效)倒计时{
mainit-=1;
如果(mainit<0){
[计时器失效];
[alertController解除ViewController激活:是完成:^{
//完成注销所需的任何操作
}]
}否则{
alertController.message=[self countDownString];
}
}
-(iAction)注销:(id)发件人{
计时器=[NSTimer scheduledTimerWithTimeInterval:1目标:自选择器:@selector(倒计时)用户信息:无重复:是];
mainit=20;
alertController=[UIAlertController]
alertControllerWithTitle:无
消息:[自倒计时字符串]
首选样式:UIAlertControllerStyleActionSheet];
UIAlertAction*cancelAction=[UIAlertAction
actionWithTitle:NSLocalizedString(@“取消”,“取消操作”)样式:UIAlertActionStyleCancel处理程序:^(UIAlertAction*操作){
[计时器失效];
NSLog(@“取消注销”);
}];
UIAlertAction*okAction=[UIAlertAction
actionWithTitle:NSLocalizedString(@“注销”,“确定操作”)样式:UIAlertActionStyleDestructive处理程序:^(UIAlertAction*操作){
[计时器失效];
NSLog(@“注销”);
//注销代码在这里
//时间到了,自动注销
}];
[alertController addAction:okAction];
[alertController添加操作:取消操作];
[alertController setModalPresentationStyle:UIModalPresentationPopover];
[self-presentViewController:alertController动画:是完成:无];
} 

您的第一个问题是您没有将
秒初始化为任何值,因此初始值为零。这就是您的警报显示“空秒”的原因。在随后的警报显示中,秒数将由
倒计时方法设置

第二个问题是,您实际上没有更新警报的
消息
属性,因此您只看到警报最初显示时
秒的值

最后,将
mainit
设置为20,然后每次定时器启动时减去1,这样它就永远不会达到0,它将是20,19,20,19,20,19

下面的代码在iAction方法中初始化了
mainit
,因此不会在层每次滴答声时重置为20。它还会更新警报的
message
属性,并在计时器达到0时解除警报(实际上它会在1秒钟后解除警报,因为我认为最好看到2..1…0…注销,但您可以更改它)

@界面视图控制器(){
内部维护;
n定时器*定时器;
UIAlertController*alertController;
}
-(NSString*)倒计时字符串{
返回[NSString stringWithFormat:@“%i秒”,mainit];
}
-(无效)倒计时{
mainit-=1;
如果(mainit<0){
[计时器失效];
[alertController解除ViewController激活:是完成:^{
//完成注销所需的任何操作
}]
}否则{
alertController.message=[self countDownString];
}
}
-(iAction)注销:(id)发件人{
计时器=[NSTimer scheduledTimerWithTimeInterval:1目标:自选择器:@selector(倒计时)用户信息:无重复:是];
mainit=20;
alertController=[UIAlertController]
alertControllerWithTitle:无
消息:[自倒计时字符串]
首选样式:UIAlertControllerStyleActionSheet];
UIAlertAction*cancelAction=[UIAlertAction
actionWithTitle:NSLocalizedString(@“取消”,“取消操作”)样式:UIAlertActionStyleCancel处理程序:^(UIAlertAction*操作){
[计时器失效];
NSLog(@“取消注销”);
- (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];

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

}