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

  • Home
  • SEARCH
  • 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 8884235
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T21:01:05+00:00 2026-06-14T21:01:05+00:00

I have a int that resets itself every time the view re-opens/leaves. I have

  • 0

I have a int that resets itself every time the view re-opens/leaves. I have tried every way of declaring the int that i can think of, from public, to instance variable to global variable, but it still seems to reset!

@interface MainGameDisplay : UIViewController
extern int theDay;

@implementation MainGameDisplay

- (void)viewDidLoad {
    [super viewDidLoad];
    NSLog(@"%i", theDay);
}

- (IBAction)returnToHome:(id)sender {
        ViewController *new = [[ViewController alloc] initWithNibName:nil bundle:nil];
        [self presentViewController: new animated:YES completion:NULL];
        NSLog(@"%i", theDay);
}

- (IBAction)theDayAdder:(id)sender {
    theDay++;
}

Okay so theDay is a global integer variable. on View load NSLog returns an output of 0. I can then click theDayAdder as many times as I want, and when I click returnToHome, it will tell me what theDay is. When I come back to MainGameDisplay page however, theDay will be reset back to zero, even though it is a global variable?

Output:
0
N (number of times you clicked 'theDayAdder' button)
0
  • 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-06-14T21:01:06+00:00Added an answer on June 14, 2026 at 9:01 pm

    The problem is that you alloc init’ing a new instance of MainGameDisplay every time you go back to it, so of course your global variable will be reset to 0. You need to create a property (typed strong) in ViewController, use that to go back to the same instance each time.

    - (IBAction)returnToGameDisplay:(id)sender {
         if (! self.mgd) {
            self.mgd = [[MainGameDisplay alloc] initWithNibName:nil bundle:nil];
         }
         [self presentViewController: self.mgd animated:YES completion:NULL];
         NSLog(@"%i", theDay);
    }
    

    In this example mgd is the property name created in the .h file.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a const int that is computed at compile time in my Managed
Where is the source code that manages View re-use in Android? I can think
If I have int someArray[100] that contains items in various elements, how can I
I have a static int that keeps an record of how many user control
I have an int property in my viewmodel that returns a custom class property.
I have a list of objects that have two int properties. The list is
I have a C function int func( int a, int* b) that I need
Let's say that I have the following: int a = 2; Number b =
I have a mysql table structure like that: id int primary key name varchar
You have a table table1 that contains id column, that is int(11), not null,

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.