How can I display text on UILabel one by one after each button click?

I have an array with text that I would like to skip, so that every time I click the button, it will display the text one by one;

however, my code cycles through the entire array with one click:

- (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 can I make it so that every time I press a button, the text is displayed one by one?

+6
source share
5 answers

Add a variable to save the current state in the class extension in the .m file of your view file, and then increment the variable each time the button is pressed:

 @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; } } 
+2
source
 @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
source

I think you can use the static variable type and the code 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; } } 
+1
source

Declare a single integer variable in the interface. int i = 0;

Make 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; } 
+1
source

You can do this by creating a member variable in the class that tracks the index with which the row was accessed and printed, as

 // declare counter int titleIndexCounter; // initialize it titleIndexCounter = 0; // manipulate it in button 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 event handler as (but this is a less preferred approach for this) -

 // manipulate it in button 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; } 
+1
source

Source: https://habr.com/ru/post/985579/


All Articles