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

时间:2015-04-21 08:45:25

标签: ios objective-c uibutton uilabel

我有一个包含文本的数组,我想循环浏览,这样每次点击我的按钮时,它都会逐个显示文本;

然而,我的代码只需按一下就会遍历整个数组:

- (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]);
    }

}

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

5 个答案:

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

答案 1 :(得分:2)

@interface myViewController ()
{
  int i;  
  NSArray *titles;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    i=0;
    titles = @[@"Label One", @"Label Two", @"Label Three",
                                 @"Label Four", @"Label 5", @"Label 6"];
}   


 - (IBAction)nextButtonOneClicked:(id)sender {

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

答案 2 :(得分:1)

我认为您可以使用static变量类型 和代码在这里

- (IBAction)buttonPressed:(id)sender {
  static int Index = 0;
  NSArray *titles = @[@"Label One", @"Label Two", @"Label Three",
                    @"Label Four", @"Label 5", @"Label 6"];
  NSLog(@"%@", titles[Index]);
  Index++;
  if( Index >= titles.count) {
      Index = 0;
 }
}

答案 3 :(得分:1)

在Interface中声明一个整数变量。 int i = 0;

执行以下代码:

-(IBAction)nextButtonClicked:(id)sender
{


 NSArray *array=@[@"Label 1",@"Label 2",@"Label 3",@"Label 4",@"Label 5",@" Label 6"];  

  label.text=[array objectAtIndex:i]; 

  NSLog(@"label %@",[array objectAtIndex: i]); 

  i=i+1; 

}

答案 4 :(得分:1)

您可以通过在类中创建一个成员变量来实现此目的,该变量跟踪用于访问和打印字符串的索引,如

// declare counter
int titleIndexCounter;

// initialize it
titleIndexCounter = 0;

// manipulate it in button's event handler as
- (IBAction)nextButtonOneClicked:(id)sender 
{
    NSArray *titles = @[@"Label One", @"Label Two", @"Label Three",
                             @"Label Four", @"Label 5", @"Label 6"];
    NSLog(@"%@", [titles objectAtIndex:titleIndexCounter]);
    titleIndexCounter++;
    if (titleIndexCounter == titles.count)
        titleIndexCounter = 0;
}

你也可以通过在按钮的事件处理程序中创建一个静态变量来做同样的事情(但这不是一个不太喜欢的方法) -

// manipulate it in button's event handler as
- (IBAction)nextButtonOneClicked:(id)sender 
{
    static int titleIndexCounter = 0;

    NSArray *titles = @[@"Label One", @"Label Two", @"Label Three",
                             @"Label Four", @"Label 5", @"Label 6"];
    NSLog(@"%@", [titles objectAtIndex:titleIndexCounter]);
    titleIndexCounter++;
    if (titleIndexCounter == titles.count)
        titleIndexCounter = 0;
}