如何在NSMutableArray中增加当前索引

时间:2013-01-25 20:20:30

标签: iphone ios6

如何增加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];

}

2 个答案:

答案 0 :(得分: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;
            }
    }

我希望你像这样使用Timer:

self.timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(yourFunctionToUpdateTimer) userInfo:nil repeats:NO];

答案 1 :(得分:0)

尝试

int index =+1;

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