开发者

Share a bool variable / NSNUmber between two view controllers

开发者 https://www.devze.com 2023-04-10 12:22 出处:网络
I have two view controllers and I want to share a bool variable between them. So I create a bool variable with a @propery (nonatomic, assign) on both sides and on the one side I wrote

I have two view controllers and I want to share a bool variable between them. So I create a bool variable with a @propery (nonatomic, assign) on both sides and on the one side I wrote

newVC.myBool1 = self.myBool2;

On the other view controller I can read the value of the passed bool variable, but I need to change it at the second view controller so I can read the value at the first view controller. So I know, this is not possible, because `bool* it is 开发者_开发问答a primitive type.

So I used NSNumber, but this also does not work. On the first view controller I set on viewDidLoad

self.myBool1 = [NSNumber numberWithBool:NO];

On the second view controller:

self.myBool2 = [NSNumber numberWithBool:YES];

But on the first view controller the value is 0 - NO... So it seems that creating the new NSNumber is not shared to the first view controller.

What can I do to solve this problem? Regards Tim


You have lots of choices, but which you should use depends on whether both viewControllers need notification of when the value changes.

If you don't need notification, the easiest choice is to use a global BOOL variable, although purists will scoff at the suggestion. But really it's two lines of code and you're done. Another option would be to store the value in NSUserDefaults.

If you need change notification in each viewController, perhaps the cleanest design is to write a "set" method in one viewController that sets the value in both itself and the other viewController. Something like:

-(void) setMyBool:(BOOL)newValue
{
  myBool = newValue;
  otherViewController.myBool = newValue;
}

If you want to change the value from either viewController, it gets a little trickier because you have to have each viewController keep a reference to the other and make sure not to recurse when setting the value. Something like:

-(void) setMyBool:(BOOL)newValue
{
  if ( self.busyFlag == YES )
     return;
  self.busyFlag = YES;
  myBool = newValue;
  otherViewController.myBool = newValue;
  self.busyFlag = NO;
}

Yet another option would be to use NSNotifications to change the value and have each viewController class listen for the change notification. And TheEye's suggestion of writing a wrapper class and keeping a reference to an instance of that class in both viewControllers would work too.

If you don't need change notifications, though, I would just create a global BOOL variable and get on with the rest of the application because it's so easy, reliable and hard to mess up.


An NSNumber object is immutable, so you can't use it like that. If you write [NSNumber initWithxxx], in fact you create a new object.

If you want to share a number or boolean between several classes, you should create your own wrapper class with setters and getters for the bool value (or subclass NSNumber). This class you can share between classes.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号