0

如何增加 currentindex 以便 textview 可以按顺序更新而不是随机更新。

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

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

self.textView.font = [UIFont fontWithName:@"Baskerville-Bold" size:22];

self.textView.textAlignment = NSTextAlignmentCenter;

self.textView.backgroundColor = [UIColor clearColor];

self.textView.textAlignment =  NSTextAlignmentCenter;
self.textView.text = @"String1";
self.textView.scrollEnabled = NO;

self.textView.editable = NO;

self.textView.userInteractionEnabled = NO;

[baseView addSubview: self.textView];

} 

- (void)updateText:(NSTimer *)theTimer {
int index = arc4random() % [myArray count];
myTextView.text = [myArray objectAtIndex:index];

}

4

2 回答 2

3

与其将索引设为局部变量,不如将其设为属性

@property(非原子)NSInteger 索引;

在 viewDidLoad 中将其初始化为 0;

然后只像这样使用它:

           - (void)updateText:(NSTimer *)theTimer 
        {
            NSLog(@"myArray count is %d",[myArray count]);
            CFShow((__bridge CFTypeRef)(myArray));

            if (index < [myArray count])
            {
                NSLog(@"%d value is %@",index,[myArray objectAtIndex:index]);
                myTextView.text = [myArray objectAtIndex:index];
                index++;
            }
            else
                index = 0;
            }
    }

我希望你像这样使用定时器:

self.timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(yourFunctionToUpdateTimer) userInfo:nil repeats:NO];
于 2013-01-25T20:46:06.067 回答
0

尝试做

int index =+1;

我已经在另一个应用程序中尝试过它并且它有效,所以它应该适合你

于 2013-01-25T20:27:04.337 回答