1

I have the following bit of code in my super viewDidLoadsection of my app:

if ( appCounter < 1   ) {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"Disclaimer", "")
                                                        message:NSLocalizedString(@"By agrreing to use this service, dont use while driving", "")
                                                       delegate:nil
                                              cancelButtonTitle:@"I Agree to not use this while Driving"
                                              otherButtonTitles: nil];
        [alert show];
        appCounter = appCounter+1;
    }

basically, it should show a disclaimer when the app loads. but every time the user navigates away from the main screen, and then comes back to the main scene (its a multiple view app) the disclaimer pops up again.

i would have thought that the app counter would have stopped this, yet it still keeps popping up.

Could someone please point out where in my code i have gone wrong? and what i need to do to rectify this?

Thank you in advance.

David Rönnqvist
  • 56,267
  • 18
  • 167
  • 205
scb998
  • 899
  • 5
  • 18
  • 42
  • 1
    How are you declaring appCounter? – LuisCien Nov 18 '13 at 22:01
  • 1
    `viewDidLoad` only gets called once when the view is loaded into memory. If this code is running every time then when you're traversing the app you're not using existing VC instances but rather making new ones. – Peter Foti Nov 18 '13 at 22:01
  • Maybe you want to popup that screen [only the first time the app is started](http://stackoverflow.com/questions/9964371/how-to-detect-first-time-app-launch-on-an-iphone/9964400#9964400)... –  Nov 18 '13 at 22:02
  • @LuisCien its declared as a NSIntegar in the .h file. – scb998 Nov 18 '13 at 22:31
  • @PeterFoti could you please elaborate on the VC instances? I'm relatively new to objective c coding and haven't come across that terminology before. – scb998 Nov 18 '13 at 22:32
  • @H2CO3 i will have a look into it and get back to you. if it fulfils my needs, re-comment as an answer and ill mark t as accepted. – scb998 Nov 18 '13 at 22:32

1 Answers1

1

Assuming you're declaring appCounter as an instance variable in your class, e.g.

@interface MyViewController () {
    int appCounter
}

Then a new MyViewController is created each time, and appCounter is reset to zero.

You want appCounter to be static: once and for all. You can replace your current version with a static variable declaration:

static int appCounter;

(i.e. your .m file, and not in the interface definition). That should be once and for all. There are other ways to get appCounter to be shared among all instances of your ViewController (some people are funny about declaring static variables, even though they aren't accessible from outside that module), but that's the easiest.

Ian
  • 3,619
  • 1
  • 21
  • 32