重复显示标签中的文本数组

时间:2013-05-16 22:31:13

标签: ios objective-c uilabel

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

- (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++;


}

现在每件事都没问题,除了没有循环从头开始再次重复查看所有文本。

任何想法都会有所帮助。

1 个答案:

答案 0 :(得分:1)

试试这个:

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

然后使static int成为一个类似的实例变量。