Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4626178
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T03:22:53+00:00 2026-05-22T03:22:53+00:00

When the user click the next button, it generate random number and I would

  • 0

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=[[NSBundle 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 ];
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T03:22:54+00:00Added an answer on May 22, 2026 at 3:22 am

    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
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

User click on a link button and it will direct them to a url
I'd like to hide a div when user click anywhere on the page outside
Example: The user login to the webpage => Click on a button This action
Details: Only disable after user clicks the submit button, but before the posting back
My scenario is to load only 7 items initially and when user clicks Next
I redirect the user to the login page when user click log out however
I want to check when the user double click on applictaion icon that no
In a web application I am working on, the user can click on a
I have an application that shows a list of items. The user can click
When a user clicks on an image on a web page, I'd like to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.