Ios 倒计时和关闭模式视图

Ios 倒计时和关闭模式视图,ios,objective-c,Ios,Objective C,我有一个模式视图,在该视图中,当用户执行某些操作时,开始倒计时。倒计时结束时,模态视图将关闭。下面是我为实现这一目标而编写的过程,但不幸的是,它导致了一些线程问题。有没有办法以一种不存在潜在线程问题的方式重写它 - (void)countDown { static int i = 3; if (i == 3) { i--; UIImage *three = [UIImage imageNamed:@"Three"]; countDo

我有一个模式视图,在该视图中,当用户执行某些操作时,开始倒计时。倒计时结束时,模态视图将关闭。下面是我为实现这一目标而编写的过程,但不幸的是,它导致了一些线程问题。有没有办法以一种不存在潜在线程问题的方式重写它

- (void)countDown {
    static int i = 3;
    if (i == 3) {
        i--;
        UIImage *three = [UIImage imageNamed:@"Three"];
        countDownFlag = [[UIImageView alloc] initWithImage:three];
        countDownFlag.frame = CGRectMake(0, 370, countDownFlag.frame.size.width, countDownFlag.frame.size.height);
        countDownFlag.center = CGPointMake(width / 2, countDownFlag.center.y);
        [self.view addSubview:countDownFlag];
        [self performSelector:@selector(countDown) withObject:nil afterDelay:0.5];
    } else if (i == 2) {
        i--;
        UIImage *two = [UIImage imageNamed:@"Two"];
        [countDownFlag setImage:two];
        [self performSelector:@selector(countDown) withObject:nil afterDelay:0.5];
    } else if (i == 1) {
        i--;
        UIImage *one = [UIImage imageNamed:@"One"];
        [countDownFlag setImage:one];
        [self performSelector:@selector(countDown) withObject:nil afterDelay:0.5];
    } else {
        [self dismissViewControllerAnimated:YES completion:nil];
    }
}
编辑:图片显示了XCode告诉我的有关问题的信息

更多编辑:

我的密码已经改变,以反映瓦迪安的答案。这是最新消息

- (void)startCountDown {
    NSLog(@"starting count down");
    counter = 3;
    [NSTimer scheduledTimerWithTimeInterval:0.5 target:self selector:@selector(countDown:) userInfo:nil repeats:YES];
}

- (void)countDown:(NSTimer *)timer {

    if (counter == 3) {
        countDownFlag = [[UIImageView alloc] init];
        countDownFlag.frame = CGRectMake(0, 370, countDownFlag.frame.size.width, countDownFlag.frame.size.height);
        countDownFlag.center = CGPointMake(width / 2, countDownFlag.center.y);
        dispatch_async(dispatch_get_main_queue(), ^{
            [self.view addSubview:countDownFlag];
        });
    } else if (counter == 0) {
        [timer invalidate];
        [self dismissViewControllerAnimated:YES completion:nil];
        return;
    }
    NSArray *imageNameArray = @[@"", @"One", @"Two", @"Three"];
    UIImage *image = [UIImage imageNamed:imageNameArray[counter]];
    dispatch_async(dispatch_get_main_queue(), ^{
        [self.countDownFlag setImage:image];
    });
    counter--;
}
我猜问题可能在于调用倒计时的代码。下面是执行此操作的代码

- (void)changeLanguage:(BOOL) isMyanmar {
    if (hasRun == YES) {
        return;
    }
    hasRun = YES;

    NSUserDefaults * userdefaults = [NSUserDefaults standardUserDefaults];
    [userdefaults setBool:isMyanmar forKey:@"myanmar"];
    [userdefaults synchronize];

    [self updateCurrentLanguageText];
    if ([self isMyanmar]) {
        [self changeLanguageFlag:@"MyanmarFlagBig"];
    } else {
        [self changeLanguageFlag:@"UnitedKingdomFlagBig"];
    }

    //>>>>>>>>>>>>>>>>>>>> the problem is probably here

    [self startCountDown];
}
更改语言代码后运行的任何代码都会失败。问题可能就在那里

编辑:线程问题已消失,但倒计时不再发生

在我将
changeLanguage
中的代码移动到调用它的方法中之后,问题就神秘地消失了。(重复但有效。)


但问题是倒计时不再发生了。

Place
static int i=3
在方法倒计时之外,按照您的方式,每次调用该方法时,它都会声明新变量。

您可以使用NSTimer执行倒计时,并使用静态变量执行计数器

方法
startCountDown
启动计时器

每0.5秒调用一次方法
倒计时:(NSTimer*)计时器。传递的NSTimer参数正是已启动的计时器。
它根据其值执行操作,并使计数器递减。如果计数器为0,则计时器无效,控制器退出

static UInt8 counter = 0;

- (void)startCountDown {
  countDownFlag = [[UIImageView alloc] init];
  countDownFlag.frame = CGRectMake(0, 370, countDownFlag.frame.size.width, countDownFlag.frame.size.height);
  countDownFlag.center = CGPointMake(width / 2, countDownFlag.center.y);
  [self.view addSubview:countDownFlag];
  counter = 3;
  [NSTimer scheduledTimerWithTimeInterval:0.5 target:self selector:@selector(countDown:) userInfo:nil repeats:YES];
}

- (void)countDown:(NSTimer *)timer {
  if (counter == 0) {
    [timer invalidate];
    [self dismissViewControllerAnimated:YES completion:nil];
    return;
  }
  NSArray *imageNameArray = @[@"", @"One", @"Two", @"Three"];
  UIImage *image = [UIImage imageNamed:imageNameArray[counter]];
  dispatch_async(dispatch_get_main_queue(), ^{
    [self.countDownFlag setImage:image];
  });
  counter--;

}

您能更详细地解释一下“导致一些线程问题”吗?所附图片显示,您的LanguageSelectionViewController在该类中找不到方法changeLanguage。重新定位
static int i=3无法解决问题
timer.userInfo=@{@“计数器”:@(计数器)}
导致错误感谢您简化了我的代码,但线程问题仍然存在。我再次编辑了代码,添加了
dispatch\u async()
块,以便在主线程上执行与UI相关的代码您的代码可能是对的。可能在别的地方。我更新了我的问题,指出了问题所在。我再次编辑了代码,将图像视图的创建置于
倒计时:
方法之外。尝试插入两行
NSLog
以缩小发生错误的行
static UInt8 counter = 0;

- (void)startCountDown {
  countDownFlag = [[UIImageView alloc] init];
  countDownFlag.frame = CGRectMake(0, 370, countDownFlag.frame.size.width, countDownFlag.frame.size.height);
  countDownFlag.center = CGPointMake(width / 2, countDownFlag.center.y);
  [self.view addSubview:countDownFlag];
  counter = 3;
  [NSTimer scheduledTimerWithTimeInterval:0.5 target:self selector:@selector(countDown:) userInfo:nil repeats:YES];
}

- (void)countDown:(NSTimer *)timer {
  if (counter == 0) {
    [timer invalidate];
    [self dismissViewControllerAnimated:YES completion:nil];
    return;
  }
  NSArray *imageNameArray = @[@"", @"One", @"Two", @"Three"];
  UIImage *image = [UIImage imageNamed:imageNameArray[counter]];
  dispatch_async(dispatch_get_main_queue(), ^{
    [self.countDownFlag setImage:image];
  });
  counter--;