How to bind an editable TextField to a read-only property

Does anyone know how to prevent Cocoa from binding to set values ​​back to the related property, but still trigger the default action selector?

I am trying to bind an NSTextField to the original NSString property on my data object. My data The object is similar to

...

@property (read-only) NSString * outCome;

- (invalid) otherMethodsAffectOutCome;

...

I bound NSTextField with the result property and in the default action handler, which I called -otherMethodAffectOutCome, and hopefully there will be / didChangeValueForKey to disable the object's property observer and return an NSTextField.

But this will not work, NSTextField will fail because it tries to set the changed text back using the setOutCome method ... I think I need the NSTextField to observe the property value change, but do not try to set the values ​​when the text changes, How do I do this to do?

Thank! -Jonny

+3
source share
2 answers

Since you already have your own model object, there are two options that are better than overriding -setValue:forUndefinedKey:.

  • Just change the property so that it does not exist readonly. Since this is an Objective-C object, it must be retained.

  • , readonly , setOutCome: ; , , - -setOutCome:. Cocoa Bindings.

    , , retain, . , nonatomic - iOS, OS X.

  • , , 2. "", "". , --: , ( .m) . .

    , @interface, , . , , @implementation.

    ?

    (retain, readonly), (retain) . @synthesize getter setter.

, 3.

#import <Foundation.h>

@interface MyClass : NSObject
{
    id _myValue;
}
@property (retain, readonly) id myValue;
@end

//
#import "MyClass.h"

@interface MyClass ()
@property (retain) id myValue;
@end

@implementation MyClass
@synthesize myValue = _myValue;
@end

, setter, , . , , , retain/release .

+1

, , setValue: forUndefinedKey: . , .

0

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


All Articles