0

我有这个 NSMutable 数组。每两秒一读并更新textview。但现在我想要的是以不同的时间间隔从 NSMutable 数组中一一读取,而不是每两秒。我怎么能做到这一点。

- (void)viewDidLoad{
myArray = [[NSMutableArray alloc]initWithObjects:@"String1",@"String2",@"String3",@"String4", @"String5",..... nil];  

[NSTimer scheduledTimerWithTimeInterval:2.0
                             target:self
                           selector:@selector(updateText:)
                           userInfo:nil
                            repeats:YES];


- (void)updateText:(NSTimer *)theTimer 
    {


        if (index < [myArray count])
        {
            myTextView.text = [myArray objectAtIndex:index];
            index++;
        }
        else
            index = 0;
        }
}

感谢帮助。

4

4 回答 4

1

很简单:每次取消计时器并以所需的时间间隔重新创建它(当然要挂在对计时器的引用上)。

于 2013-01-28T20:09:15.763 回答
1

如果您只需要在每次加载视图时更改间隔值,您可以尝试使用随机数,在下一个示例中,它将返回 2 到 10 之间的随机数:

[NSTimer scheduledTimerWithTimeInterval:arc4random() % 10 + 2
                             target:self
                           selector:@selector(updateText:)
                           userInfo:nil
                            repeats:YES];
于 2013-01-28T20:25:42.017 回答
1

在您的 updateText 选择器中,只需创建一个具有不同间隔的新 NSTimer ,直到数组中没有更多对象。

于 2013-01-28T20:11:51.263 回答
0

使用 performSelector:withObject:afterDelay,但使用随机延迟!

[self performSelector:@selector(updateText:) withObject:nil afterDelay:arc4random()%10+2];

于 2013-01-28T20:58:36.843 回答