Reputation: 909
I have the following bit of code in my super viewDidLoad
section 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.
Upvotes: 1
Views: 218
Reputation: 3709
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.
Upvotes: 1