0

VC1.m contains the following code after a process completes.

if (self.iboard>0) {
             countIsNil = TRUE;
         } 

I want another VC, say VC2.m to include the following code.

- (void)viewDidLoad
{
    if (countIsNil)
    {
        countIsNil = FALSE;
        count.text=@"1";
        return;

    }
    [super viewDidLoad];
}

How do I construct/manage the BOOL countIsNil so that this works? I suspect the answer involves using static variables, but I have no examples of such.

zerowords
  • 2,915
  • 4
  • 29
  • 44
  • 1
    Does this help? http://stackoverflow.com/a/9736559/1043198 – Ant P May 28 '13 at 14:08
  • possible duplicate of [Accessing variable from different View Controllers](http://stackoverflow.com/questions/11575749/accessing-variable-from-different-view-controllers) – Hot Licks May 28 '13 at 14:46

2 Answers2

2

There are two solutions:

  1. You need to make a property for this variable.
  2. You can maintain using NSUserDefaults class.

Example for NSUserDefaults, written in VC1:

[[NSUserDefaults standardUserDefaults]setBool:YES forKey:@"countIsNil"]
    [[NSUserDefaults standardUserDefaults] synchronize];

Write in VC2:

- (void)viewDidLoad
{
    if ([NSUserDefaults standardUserDefaults] valueForKey:@"countIsNil"])
    {
        countIsNil = FALSE;
        count.text=@"1";
        return;
    }
    [super viewDidLoad];
}
timss
  • 9,982
  • 4
  • 34
  • 56
Dharmbir Singh
  • 17,485
  • 5
  • 50
  • 66
  • 1
    NSUserDefaults is probably not the option here. That's only useful if you want these variables persisting across app launches. Use a singleton class instead and encapsulate your variables there. – Richard J. Ross III May 28 '13 at 14:42
  • I was able to make the NSUserDefaults method work after adding a ; to line one and ] to line 2. But, if that is not a good option, then I need to know more about "solution" 1 or your singleton class "option", please. Is "solution" 1 suggesting that somehow making a property for this variable will make it easier to "pass it forward"? If so, what changes would I need to make in VC1, then? – zerowords May 28 '13 at 15:49
-1

Just take your BOOL variable globally i mean to say that take it in AppDelegate file, set its properties and synthesize it, Now you can pass value in this variable from any class and retrieve it from any where.. here is example:

 // AppDelegate.h
 @interface AppDelegate : UIResponder <UIApplicationDelegate>{

     BOOL countIsNil;
 }
 @property(readwrite)BOOL countIsNil;
 @end   

 // AppDelegate.m
 @implementation AppDelegate
 @synthesize countIsNil
Irshad Mansuri
  • 397
  • 2
  • 12
  • 1
    Putting variables in the app delegate is a terrible option, I don't know why people recommend it. Create a proper singleton and use that instead. – Richard J. Ross III May 28 '13 at 14:43
  • If you want that variable in more then one views then its very useful and i have also mention it that u can use that variable in any class if u want to use that variable in single class then u can directly pass it while navigation to next view.. – Irshad Mansuri May 29 '13 at 06:37