How do I display text on UILabel one-by-one after each click of my button?

I have an array with text that I would like to cycle through, so that each time I click my button it will display the text one-by-one;

however, my code loops through the entire array with one press:

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

}

How would I make it so that every time I click my button is displays the text one-by-one?

Upvotes: 6

Views: 1044

Answers (5)

Ashok Londhe
Ashok Londhe

Reputation: 1491

Declare one integer variable in Interface. int i=0;

Do the following code:

-(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; 

}

Upvotes: 1

Sanjay Mohnani
Sanjay Mohnani

Reputation: 5967

You can do so by creating a member variable in the class which keeps track of the index using which the string was accessed and printed, as

// 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;
}

you can also do the same by creating a static variable inside the button's event handler as (but it's a less preferred approach to do so)-

// 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;
}

Upvotes: 1

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 726589

Add a variable to keep the current state to the class extension in your view controller .m file, then increment the variable each time the button is clicked:

@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;
    }
}

Upvotes: 2

user4810968
user4810968

Reputation:

@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;
     }
}

Upvotes: 2

Yılmaz G&#252;rsoy
Yılmaz G&#252;rsoy

Reputation: 162

i think you can use static variable type and code is here

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

Upvotes: 1

Related Questions