1

我需要在界面生成器中设置控件的启用属性,这取决于他的偏好中的 2 个布尔值。

但是,运算符应该是 OR 而不是 AND。如果两者之一为真,则应启用我的控件。

目前,我只能使用 AND 运算符使其工作,(见截图)。

在此处输入图像描述

谢谢

4

2 回答 2

1

不幸的是,在 IB 中,你被 and 困住了。我的建议是向您的 NIB(可能是您的 NIB 的所有者)可访问的对象添加一个新属性,这取决于对其他对象的更改以启用您的控制/视图。

看起来您正在使用Shared User Defaults Controller,所以我建议您在所有者中为您的组合用户默认值(可能downloadingCastOrCrew)创建一个新的布尔属性,然后您需要确保当任何一个默认值更改时,您更改 的值downloadingCastOrCrew

在您的界面中:

@property BOOL downloadingCastOrCrew;

在设置控制器或 awakeFromNib 之后的实现中:

 [[NSUserDefaultsController sharedUserDefaultsController] addObserver:self
      forKeyPath: @"values.kSearchPreferencesDownloadCast"
      options:NSKeyValueObservingOptionNew
      context:NULL];
 [[NSUserDefaultsController sharedUserDefaultsController] addObserver:self
      forKeyPath: @"values.kSearchPreferencesDownloadCrew"
      options:NSKeyValueObservingOptionNew
      context:NULL];

在您拆除控制器的实现中: [[NSUserDefaultsController sharedUserDefaultsController] removeObserver: self forKeyPath: @"values.kSearchPreferencesDownloadCast"];

 [[NSUserDefaultsController sharedUserDefaultsController] removeObserver: self
      forKeyPath: @"values.kSearchPreferencesDownloadCrew"];

如果您还没有观察者,请添加观察者:

 - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object 
      change:(NSDictionary *)change context:(void *)context
 {
     NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
     self.downloadingCastOrCrew = [defaults boolForKey: @"kSearchPreferencesDownloadCast"] 
         || [defaults boolForKey: @"kSearchPreferencesDownloadCrew"];
 }

通过使用访问器方法,您将触发 kvo 并且您将能够使用控制器downloadingCastOrCrew作为您的布尔值来检查而不是直接检查 NSUserDefaults 值。

显然,如果您已经有一个 observeValueForKeyPath,您可能希望在调用中添加一个上下文值并在addObserver:forKeyPath:options:context调用中检查它observeValueForKeyPath:ofObject:change:context

于 2013-03-19T10:14:12.603 回答
0

你可以这样做:

创建第三个属性

@property BOOL isFirst;
@property BOOL isSecond;

@property BOOL isTextFieldVisible;//this one is your third

- (IBAction)isSec:(id)sender;
- (IBAction)isFir:(id)sender;

在实施中

- (id)init
{
    self = [super init];
    if (self) {
        self.isFirst=NO;
        self.isSecond=NO;
    }
    return self;
}

- (IBAction)isSec:(id)sender {
    self.isSecond=!self.isSecond;
    [sender setTitle:[NSString stringWithFormat:@"isSecond: %d",self.isSecond]];

    self.isTextFieldVisible=self.isFirst || self.isSecond;

    self.isTextFieldVisible=!self.isTextFieldVisible;

    NSLog(@"->%d",self.isTextFieldVisible);
}

- (IBAction)isFir:(id)sender {
    self.isFirst=!self.isFirst;
    [sender setTitle:[NSString stringWithFormat:@"isfirst: %d",self.isFirst]];

    self.isTextFieldVisible=self.isFirst || self.isSecond;

     self.isTextFieldVisible=!self.isTextFieldVisible;

    NSLog(@"->%d",self.isTextFieldVisible);
}
@end

在绑定中,只需将 textField 绑定到第三个属性,

在此处检查正在运行的应用程序。

编辑1:

NSNegateBoolean在 IB 中更改 ValueTransformer 。这样我的两行在self.isTextFieldVisible=!self.isTextFieldVisible;IBAction 中都不需要。

于 2013-03-19T10:42:58.310 回答