ios - 永远重复显示标签中的文本数组

标签 ios objective-c uilabel

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

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

}

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

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


}

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

任何想法都会有帮助。

最佳答案

试试这个:

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

然后将 static int 设为实例变量,就像它应该的那样。

关于ios - 永远重复显示标签中的文本数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16598580/

相关文章:

ios - 从其他类iOS获取变量(位置信息)

iphone - xml文件的CDATA中的下标和上标。使用UILabel显示已解析的XML内容

iphone - 在 NavigationBar 中为 ABPeoplePickerNavigationController 自定义标题

ios - 在 cocos2d 中显示不滞后或卡顿的加载图形

ios - 没有这样的表..无法执行查询…xcode sqlite

ios - 合并一种类型的可观察量,然后链接到另一种类型的可观察量

ios - Swift 中的多个选择器 View

ios - UIView 卡在 UITableview header 后面

ios - 为tableview中的不同文本字段重用相同的pickerview

fonts - 如何使用 Swift 创建属性字符串?