NSMenuItem binds a value to BOOL

I am having some problems linking NSMenuItem binding to BOOL.

I simplified the problem:

1) A menu item should call an action method that changes the BOOL value, otherwise it will not work (i.e. if NSButton calls a method that changes the BOOL value, the menu item will not be updated)

2) Even if the action method makes BOOL constant (i.e. enabled = YES), the "value" of the menu item still alternates.

Any ideas? I'm so confused!

Here is the code:

MenuBindings_AppDelegate.h

#import <Cocoa/Cocoa.h>

@interface Menu_BindingsAppDelegate : NSObject <NSApplicationDelegate> 
{   
   BOOL foo;
}

- (IBAction)toggle:(id)sender;
- (IBAction)makeYes:(id)sender;

@property BOOL foo;

@end

Menu_BindingsAppDelegate.m

@implementation Menu_BindingsAppDelegate

@synthesize foo;

- (IBAction)toggle:(id)sender
{
   [self setFoo:!foo];
}

- (IBAction)makeYes:(id)sender
{   
   [self setFoo:YES];
}

@end

In my nib, I have a button associated with the -makeYes: action and a menu item associated with the -toggle: action. The "value" binding of the menu item is bound to the "foo" attribute of the application delegation.

Thank.

+3
1

Cocoa Key-Value Observing (KVO) . ( BOOL) , , , , Key -Value Coding - . ivar , KVO .

KVC- , , @synthesize , .

KVC- :

//YourModel.h
@interface YourModel : NSObject
{
    BOOL enabled;
}
- (BOOL)enabled;
- (void)setEnabled:(BOOL)flag;
@end

//YourModel.m
@implementation YourModel
- (BOOL)enabled
{
    return enabled;
}
- (void)setEnabled:(BOOL)flag
{
    enabled = flag;
}
@end

, Objective-C 2.0:

//YourModel.h
@interface YourModel : NSObject
{
    BOOL enabled;
}
@property BOOL enabled;
@end

//YourModel.m
@implementation YourModel
@synthesize enabled;
@end

[yourModel setEnabled:YES], KVO ( ) .

yourModel.enabled = YES, KVC, .

, , .

+2

Source: https://habr.com/ru/post/1732306/


All Articles