1

So I have this declared in my ViewController.h:

@property (nonatomic) BOOL areThereAds;

How can I access this variable in my AppDelegate.m class so that I can load a different storyboard with a different layout if areThereAds == NO.

Something like:

UIStoryboard *storyboard;

if (areThereAds == NO){
   storyboard == [UIStoryboard storyboardWithName:@"Main_iPhone-noAds" bundle:nil];
}
else{
   storyboard == [UIStoryboard storyboardWithName:@"Main_iPhone" bundle:nil]
}
1
  • Is the decision of whether or not there are ads made by the ViewController (i.e. it's a display decision) or the application delegate (..it's an application-wide issue)? Commented Aug 14, 2014 at 21:07

2 Answers 2

2

Another way to access a boolean throughout the app is to store it in [NSUserDefaults standardUserDefaults] as follows:

[[NSUserDefaults standardUserDefaults] setBool:NO forKey:@"isCompleted"];
[[NSUserDefaults standardUserDefaults] synchronize];

To retrieve it from any class

BOOL someBool = [[NSUserDefaults standardUserDefaults] boolForKey:@"isCompleted"];
Sign up to request clarification or add additional context in comments.

Comments

0

When I was working with passing data related to an external API response I created a singleton class which I could include the header of in each ViewController implementation file and then subsequently get the singleton instance and then retrieve its values.

For example:

Inside ViewController1.h:

#include "SingletonClass.h"

Inside ViewController1.m:

[[SingletonClass getClassInstance] someBool:YES];

Inside the header file of a class you want to retrieve the variable from:

#include "SingletonClass.h"

Inside the implementation file of a class you want to retrieve the variable from:

bool someBool =  [[SingletonClass getClassInstance] someBool]
if (someBool) {

} else {

}

I am not in front of my Mac at the moment so this may not be totally syntactically correct, but I hope it may give you an idea of how you could accomplish your question

Note: IMO the accepted answer's use of NSUserDefaults is bad practice as it is a system wide database of user preferences, assigning application specific values to it feels like using it for something it wasn't meant for. (Do we want a Windows-style registry usage on Mac?)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.