1

I have one mutable array in which data comes dynamically. For that I have one UITextField and one add UIButton. UITextField accepts only numeric data.

When I click add button, data entered as below.

[1,5,6,2,1,5,3,4,........ and so on..

There are two buttons next and previous.

So, what I want is when I click previous button the entered data must be displayed sequentially in reverse order and same time if click next button it must be displayed in forward direction.

iGatiTech
  • 2,306
  • 1
  • 21
  • 45

4 Answers4

4

Use NSSortDescriptor

   NSSortDescriptor *aSortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"self" ascending:NO];
   NSArray *aArrSortDescriptor = [NSArray arrayWithObject:aSortDescriptor];
   NSArray *aArrSorted = [YourArray sortedArrayUsingDescriptors:aArrSortDescriptor];
   NSLog(@"%@",aArrSorted);

Have a look at the documentation and for more info see this.

Community
  • 1
  • 1
Yasika Patel
  • 6,356
  • 3
  • 19
  • 21
3

You need to hold the index point to get this.increment and decrement the index with previous and next button clicks,

- (IBAction)previousClicked:(id)sender {
    if (index != 0) {
        index--;
        self.inputTextField.text = [self.dataArray objectAtIndex:index];
    }
}

- (IBAction)addCLicked:(id)sender {
    [self.dataArray addObject:[NSString stringWithFormat:@"%@",self.inputTextField.text]];
    index = self.dataArray.count;
     self.inputTextField.text = @"";
}

- (IBAction)nextClicked:(id)sender {
    if (index < self.dataArray.count) {
        self.inputTextField.text = [self.dataArray objectAtIndex:index];
        index++;
    }
}
pidjay
  • 28
  • 5
Rameswar Prasad
  • 1,331
  • 17
  • 35
1

when you click previous button ,you need to do like

NSSortDescriptor *Lowest = [NSSortDescriptor sortDescriptorWithKey:@"self" ascending:NO];
[mutableArrayOfNumbers sortUsingDescriptors:[NSArray arrayWithObject:Lowest]];
Sport
  • 8,570
  • 6
  • 46
  • 65
1

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!

pidjay
  • 28
  • 5
  • Actually my array is also not fixed. generated dynamically. – iGatiTech Aug 24 '14 at 15:52
  • Well. I was not trying to hand you with a finished work here, but just explaining how it should work (as I understood at least) with compilable code. – pidjay Aug 24 '14 at 16:15