如何从for循环中设置文本?

时间:2016-05-07 13:47:38

标签: objective-c

我有一个数组:

@[2:00 am, 3:00 am, 4:00 pm, 5:00 pm, 6:00 pm];

假设当前时间是下午4:00,我希望文本5:00 pm在UILabel中打印,因为它大于4:00 pm

所以我使用了下面的for循环:

for (int i=0;  i < prayerTimesArray.count ; i++) {

    NSTimeInterval arrayTime = [[prayerTimesArray objectAtIndex:i] timeIntervalSince1970];
    NSLog(@"The Fajr Time In Miliseconds is %f",arrayTime);

    if(currentTime < arrayTime)
    {
        _ramadhanTimeLabel.text=[prayerTitleArray objectAtIndex:i];
        [self setCustomProgressViewinBootom];
        progressViewBootom.progress=currentTime/arrayTime;
    }
}

但它会在标签上打印6:00 pm,因为它再次循环,但我需要打印5:00 pm

1 个答案:

答案 0 :(得分:0)

break设置为标签的文字后,您需要使用5:00 pm退出循环:

这是稍微改进的(更易读)版本的循环:

for (NSDate *date in prayerTimesArray) {
  NSTimeInterval arrayTime = [date timeIntervalSince1970];
  NSLog(@"The Fajr Time In Miliseconds is %f", arrayTime);

  if(currentTime < arrayTime) {
    _ramadhanTimeLabel.text = [NSString stringWithFormat:@"%f", arrayTime];
    [self setCustomProgressViewinBootom];
    progressViewBootom.progress = currentTime / arrayTime;
    break;
  }
}