English 中文(简体)
两种观点控制器之间共享一个组合变量/NSNUmber
原标题:Share a bool variable / NSNUmber between two view controllers

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.

因此,我使用了<条码>NSNumber,但这也不可行。 关于第一种看法,控制器一在<代码>上设定

self.myBool1 = [NSNumber numberWithBool:NO];

关于第二个观点,控制者:

self.myBool2 = [NSNumber numberWithBool:YES];

但第一种看法是控制器的价值为0-NO......。 因此,创建新的<代码>NSNumber似乎不向第一观察控制员分享。

What can I do to solve this problem? Regards Tim

最佳回答

你们有许多选择,但你们应当使用这些选择取决于这两种观点是否都存在。 主计长需要通知价值变动的时间。

如果你不需要通知,那么最容易的选择是使用全球<代码>BOOL变量,尽管pur家会根据建议放弃。 但是,这实际上有两条法典,你也做了调整。 另一种选择是将价值储存在上。

如果每个观点都需要变更通知,则最清洁的设计或许是在一种观点的主计长中写出一种“定型”方法,确定自己和其他观点的主计长的价值。 类似:

-(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;
}

另一种选择是使用<代码>NS Notifications,以改变价值,并具有每一种观点 主计长班级听取变更通知。 而Eye建议撰写一个包裹班,同时在双眼中都提及这一类。

如果你不需要更改通知,那么我就只会产生一个全球<代码>BOOL的变量,并随着申请的其余部分的出现,因为它很容易、可靠和难以做到。

问题回答

物体不可变更,因此你可以照此办理。 如果你写上<条码>[NSNumber initWithxxx],事实上,你会制造一个新的物体。

如果你想在几个班子之间分享<代码>>或boolean,那么你就应当创建自己的包装类别,配制器和包装值(或子级NSNumber)。 这一班级可分成几类。





相关问题
List Contents of Directory in a UITableView

I am trying to list the contents of Ringtones directory in a TableView, however, I am only getting the last file in the directory in ALL cells, instead of file per cell. This is my code: - (...

iPhone NSUserDefaults persistance difficulty

In my app i have a bunch of data i store in the NSUserdefaults. This information consists of an NSObject (Object1) with NSStrings and NSNumbers and also 2 instances of yet another object (Object2). ...

Writing a masked image to disk as a PNG file

Basically I m downloading images off of a webserver and then caching them to the disk, but before I do so I want to mask them. I m using the masking code everyone seems to point at which can be found ...

Resize UIImage with aspect ratio?

I m using this code to resize an image on the iPhone: CGRect screenRect = CGRectMake(0, 0, 320.0, 480.0); UIGraphicsBeginImageContext(screenRect.size); [value drawInRect:screenRect blendMode:...

Allowing interaction with a UIView under another UIView

Is there a simple way of allowing interaction with a button in a UIView that lies under another UIView - where there are no actual objects from the top UIView on top of the button? For instance, ...

热门标签