在两个视图控制器之间共享 bool 变量 / NSNUmber

发布于 2024-12-07 22:21:24 字数 611 浏览 1 评论 0原文

我有两个视图控制器,我想在它们之间共享一个 bool 变量。 因此,我创建了一个 bool 变量,两侧都有 @propery (非原子,分配),一侧我写

newVC.myBool1 = self.myBool2;

在另一个视图控制器上,我可以读取传递的 bool 变量的值,但我需要在第二个更改它视图控制器,这样我就可以读取第一个视图控制器的值。 所以我知道,这是不可能的,因为 `bool* 它是一个原始类型。

所以我使用了NSNumber,但这也不起作用。在第一个视图控制器上,我设置了 viewDidLoad

self.myBool1 = [NSNumber numberWithBool:NO];

在第二个视图控制器上:

self.myBool2 = [NSNumber numberWithBool:YES];

但是在第一个视图控制器上,该值是 0 - NO...所以看来创建新的 NSNumber code> 不共享给第一个视图控制器。

我可以做什么来解决这个问题? 问候蒂姆

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

末が日狂欢 2024-12-14 22:21:24

您有很多选择,但应该使用哪个取决于两个 viewController 是否都需要值更改时的通知。

如果您不需要通知,最简单的选择是使用全局 BOOL 变量,尽管纯粹主义者会嘲笑这个建议。但实际上只需两行代码就可以完成。另一种选择是将值存储在 NSUserDefaults 中。

如果您需要在每个视图控制器中进行更改通知,也许最简洁的设计是在一个视图控制器中编写一个“set”方法,该方法在其自身和另一个视图控制器中设置值。例如:

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

如果您想更改任一视图控制器的值,则会变得有点棘手,因为您必须让每个视图控制器保留对另一个视图控制器的引用,并确保在设置值时不要递归。类似于:

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

另一种选择是使用 NSNotifications 来更改值并让每个 viewController 类侦听更改通知。 TheEye 的建议是编写一个包装类并在两个 viewController 中保留对该类实例的引用,这也是可行的。

不过,如果您不需要更改通知,我只需创建一个全局 BOOL 变量并继续处理应用程序的其余部分,因为它非常简单、可靠且难以搞乱。

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.

难理解 2024-12-14 22:21:24

NSNumber 对象是不可变的,因此您不能这样使用它。如果你写[NSNumber initWithxxx],实际上你创建了一个新对象。

如果您想在多个类之间共享 numberboolean,您应该创建自己的包装类,其中包含 bool 值的 setter 和 getter(或子类 NSNumber< /代码>)。您可以在班级之间共享该课程。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文