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; }
source share