Re: Programmatically loading an NSImage via other field
Re: Programmatically loading an NSImage via other field
- Subject: Re: Programmatically loading an NSImage via other field
- From: Ken Tozier <email@hidden>
- Date: Thu, 4 Dec 2008 10:38:52 -0500
My example wasn't, strictly speaking, KVO. To set up an observer you
would need to do something like the following
@interface
MYController : NSObject
{
IBOutlet NSTextField *imagePath;
}
@end
@implementation
- (id) init
{
self = [super init];
if (self)
{
[self addObservers];
}
return self;
}
- (void) addObservers
{
[imagePath addObserver: self
forKeyPath: @"value"
options: NSKeyValueObservingOptionOld | NSKeyValueObservingOptionNew
context: NULL];
}
- (void) observeValueForKeyPath:(NSString *) inKeyPath
ofObject:(id) inObject
change:(NSDictionary *) inChange
context:(void *) inContext
{
if ([inKeyPath isEqualToString: @"value"])
{
if (inObject == imagePath)
{
NSString *oldValue = [[inChange objectForKey:
NSKeyValueChangeOldKey] stringValue],
*newValue = [[inChange objectForKey: NSKeyValueChangeNewKey]
stringValue];
/* decide whether the difference between oldValue and newValue
warrants loading a new image */
}
}
}
I'm not 100 percent certain you can bind to the value of a text field
this way (haven't tried it) but the above is the way you observe
properties of objects. Another approach would be to write a subclass
of NSTextField and override the "setValue" method. That way, whenever
something tries to change the field, you know about it.
- (void) setValue:(id) inVaue
{
[super setValue: inVaue];
/* do image load here if change warrants an action */
}
As to dragging connectors, that doesn't set up a binding, it basically
just associates a GUI element with a property of your controller. It
just says this text field corresponds with this outlet in my
controller. It gives you a way to refer to specific gui objects in
your code which you can see used in "addObservers" above. There are no
actions associated with this connection you have to establish the
behavior yourself using bindings, kvo, subclassing etc
On Dec 4, 2008, at 5:55 AM, email@hidden wrote:
In response to all the responses...
This "monitoring" needs to happen whether the user types in a field,
or, there is a choice made via NSOpenPanel, or the record is
scrolled (meaning moved next, moved previous).
So, using the beginning of the KVO below (which is the route I
suspected I'd have to go, but not sure exactly how), would I need to
add other delegate methods to handle the other cases?
Also, is dragging the delegate from the text field to the File's
Owner the same as the awake from NIB call, or do I need to add the
awakFromNIB as well?
Thanks!
Peace, Love, and Light,
/s/ Jon C. Munson II
Quoting Ken Tozier <email@hidden>:
Something like this should get you in the ballpark
MyController : NSObject
{
IBOutlet NSTextField *imagePathField;
IBOutlet NSImageView *imageView;
}
// register controller to receive messages from the text field
- (void) awakeFromNib
{
[imagePathField setDelegate: self];
}
// when user hits return key, the text field calls this handler
allowing you to respond
- (void) controlTextDidEndEditing:(NSNotification *) inNotification
{
// check message type
if ([[inNotification name] isEqualToString:
NSControlTextDidEndEditingNotification])
{
// type OK, check which object sent message. (can be used to
respond
to multipe text fields)
if ([inNotification object] == imagePathField)
{
// get text from field and try to load it into a new image
NSImage *tempImage = [NSImage imageWithContentsOfFIe:
[imagePathField stringValue]];
// set the image in the image view
if (tempImage != nil)
[imageView setImage: tempImage];
}
}
}
On Dec 3, 2008, at 10:23 PM, email@hidden wrote:
Namaste!
OK, I'm beat for the day...
I'm struggling with how to load an image into an NSImage via
another field's value.
I have a field that contains a path & name of an image file. The
user can type it in or select it via OpenFile sheet. This works
just fine and I'm setting the underlying data object so the value
persists.
Now, my question: how do I get that value to propagate over to
the image?
In Windows, I can monitor via the onChange event for the
textbox. Piece of cake.
Here, though, I'm a bit perplexed.
I thought perhaps the textDidChange delegate method might do it
(I put that in my File's Owner, and made the File's Owner a
delegate of the NSTextField that contains the image name).
However, it doesn't fire e'en though the text clearly changes
(onscreen).
So, how do I accomplish this should-be-easy feat?
Thanks!
Peace, Love, and Light,
/s/ Jon C. Munson II
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden