每次点击我的按钮后,如何在UILabel上逐一显示文字?

Ful*_*Fan 6 objective-c uibutton uilabel ios

我有一个文本数组,我想循环,所以每次我点击我的按钮,它将逐个显示文本;

但是,我的代码循环遍历整个数组:

- (IBAction)nextButtonOneClicked:(id)sender {

    NSArray *titles = @[@"Label One", @"Label Two", @"Label Three",
                             @"Label Four", @"Label 5", @"Label 6"];

    for (int i=0; i<[titles count]; i++) {
        NSLog(@"%@", titles[i]);
    }

}
Run Code Online (Sandbox Code Playgroud)

我如何制作它以便每次单击我的按钮时都会逐个显示文本?

das*_*ght 2

添加一个变量以将当前状态保留到视图控制器 .m 文件中的类扩展中,然后在每次单击按钮时递增该变量:

@interface MyViewController() {
    int _currentTitle;
    NSArray *_titles;
}
@end

-(instancetype)initWithCoder:(NSCoder *)decoder {
    if (self = [super initWithCoder:decoder]) {
        _currentTitle = 0;
        _titles = @[@"Label One", @"Label Two", @"Label Three",
                             @"Label Four", @"Label 5", @"Label 6"];
    }
    return self;
}

- (IBAction)nextButtonOneClicked:(id)sender {
    NSString *str = _titles[_currentTitle++];
    NSLog(@"%@", str);
    myLabel.text = str;
    if (_currentTitle == _titles.count) {
        _currentTitle = 0;
    }
}
Run Code Online (Sandbox Code Playgroud)