开发者

How to add value into array overtime user click on next button in xcode?

When the user click the next button, it generate random number and I would like to store the number into the array. My array is storing the last number only. Should I initialize the array outside the 'next' function? Moreover, I would like the 'back button' to read the array from the last in number. Please advise.

- (IBAction)Next:(id)sender {

    // Do any additional setup after loading the view from its nib.

    //generate random number - result is a range of 0-10   
    int randomnumber = (arc4random() % 10);

   // Add the random number into array
   [myArray addObject:[NSNumber numberWithInt:randomnumber]];

    // easy way to look what is now in the array
        NSLog([myArray description]);

     NSString *fileName = [NSString stringWithFormat:@"File_no_%d", randomnumber +1];

    //render a complete file-path out of our filename, the main-bundle and the file-  extension

    NSString *filePath=[[NSBun开发者_StackOverflow社区dle mainBundle] pathForResource:fileName ofType:@"txt"];

    //fetch the text content from that file

    NSString *myText= [NSString stringWithContentsOfFile:filePath
                                                encoding:NSUTF8StringEncoding
                                                   error:nil];

    //hand that text over to our textview

    TextView.text=myText;
}

- (IBAction)Back:(id)sender {

    NSNumber *last_array_num = [myArray objectAtIndex:myArray.count - 1];

   // read the file name based on the last number in array

    NSString *fileName = [NSString stringWithFormat:@"File_no_%d", last_array_num ];
}


You say you are initializing the array within the Next method as well as adding to it? If so, the array needs to be initialized outside that method, and just once. This keeps your data intact, otherwise it is overwritten when you initialize it the next time.

You are adding to the array just fine, so that doesn't need to change at all. As for reading the number in your Back method, you simply need to have the following line of code:

EDIT: this is the code you would use to get the results you want from your array. Also, the formal approach is not to start with capital letters in your code unless it defines a class (like NSString). For methods, like this, you should use something like - (IBAction)backButton:(id)sender instead. It's not a big deal at all and your code will work fine, but it's just etiquette and keeps your code a bit less confusing in the long run. I have a feeling someone might say something about it later, so I'm just letting you know ahead of time. Anyway, here is the code you want

SECOND EDIT: Like you were thinking, you should make a variable that can be read from in the code. In your header file, add this

int arrayCount;

In your code, once myArray is created, set arrayCount

arrayCount = [myArray count];

This should also be done if add or remove any objects from the array.

Then in your action method, you can call the files

- (IBAction)Back:(id)sender {
    NSString *filePath = [NSString stringWithFormat:@"File_no_%d", [[myArray objectAtIndex:arrayCount - 1] intValue]];

    // make sure you aren't going beyond the bounds of the array;
    if (arrayCount > 1) {
        // decrease the count of the arrayCount;
        arrayCount--;
    }
}

Using this should allow you to move backwards in your array one step each time you click the button. Again, if this isn't what you are looking for, just let me know and we will get to the bottom of it

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜