0

textviewをランダムではなくシーケンス方式で更新できるようにcurrentindexをインクリメントする方法。

- (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 に答える