Not sure what you are asking here, but I can't comment on your question. So I'll answer the question I understood.
First, to be clear, here is what I understood. Given an array with NSNumber
(or is it NSString
?) such as @[@1, @5, @6, @2, @1, @5]
, you'd like to get the number you entered before, and so on each time to tap on the previous button. And the next one when tapping on next button. Am I correct?
If so, here is the answer.
@interface SomeViewController ()
{
NSArray *numbers;
NSInteger currentIndex;
}
@end
@implementation SomeViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Init the array and the index with demo values
numbers = @[@1, @5, @6, @2, @1, @5];
currentIndex = 3; // that is @2
NSLog(@"Simulate a tap on the previous button, twice in a row");
[self tappingPreviousButton]; // 6
[self tappingPreviousButton]; // 5
NSLog(@"Simulate a tap on the next button, twice in a row");
[self tappingNextButton]; // 6
[self tappingNextButton]; // 2
// this will print the sequence 6 and 5, then 6 and 2
}
return self;
}
- (void)tappingPreviousButton
{
currentIndex = MAX(currentIndex - 1, 0); // prevent the index to fall below 0
NSLog(@"%@", numbers[currentIndex]);
}
- (void)tappingNextButton
{
currentIndex = MIN(currentIndex + 1, [numbers count] - 1); // prevent the index to go above the number of items in your array
NSLog(@"%@", numbers[currentIndex]);
}
@end
The trick is to have a variable tracking the index of the array you are at. You can then remove one (previous) or add one (next) to get the value you want in the array.
Hope that helped!