0

我试图在 xcode 中永远重复显示标签中的文本数组,首先我添加了一个arrayinviewDidLoad方法

- (void)viewDidLoad
{
[super viewDidLoad];
_tweettest = @[@"tweet1",
 @"tweet2",
 @"tweet3",
 @"tweet4",
 @"tweet5"];

}

然后我在方法中添加NSTimer对象viewDidAppear

-(void) viewDidAppear:(BOOL)animated
{
 [super viewDidAppear:animated];
 NSDate *d = [NSDate dateWithTimeIntervalSinceNow: 0.1];
  Timer1 = [[NSTimer alloc] initWithFireDate: d
 interval: 1
 target:self
 selector:@selector(Timer1Tasks)
 userInfo:nil
repeats:YES];
NSRunLoop *runner = [NSRunLoop currentRunLoop];
[runner addTimer:Timer1 forMode: NSDefaultRunLoopMode];

}

然后添加每1秒将文本添加到标签的方法

-(void) Timer1Tasks
{
static int count = 0;
NSString* letter = nil;


switch(count) {

    case 0: letter = _tweettest[0];
        _tweetlabel.text = [NSString stringWithFormat:@"%@",  letter];
        break;
    case 1: letter = _tweettest[1];
        _tweetlabel.text = [NSString stringWithFormat:@"%@",  letter];
        break;
    case 2: letter = _tweettest[2];
        _tweetlabel.text = [NSString stringWithFormat:@"%@",  letter];
        break;
    case 3: letter = _tweettest[3];
        _tweetlabel.text = [NSString stringWithFormat:@"%@",  letter];
        break;
    case 4: letter = _tweettest[4];
        _tweetlabel.text = [NSString stringWithFormat:@"%@",  letter];
        break;
}
count++;


}

现在一切都很好,除了没有循环从头开始永远重复查看所有文本。

任何想法都会有所帮助。

4

1 回答 1

1

尝试这个:

-(void) Timer1Tasks
{
static int count = 0;
_tweetlabel.text = _tweettest[count];
count = ((count + 1) %  _tweettest.count);
}

然后将静态 int 设为应有的实例变量。

于 2013-05-16T22:44:47.167 回答