Re: NSTextField sendActionOn:
Re: NSTextField sendActionOn:
On 17 Jan 2010, at 16:30, Brad Stone wrote:
> Here's why I need this - I've been trying to solve this problem for 2 weeks!
>
> This issue all revolves around a NSCollectionView. Each CollectionViewItem has a view containing a NSTextField, NSDatePicker, NSButton (checkbox), and an NSLevelIndicator. The selection index of my CollectionView is binded to the selection index of an NSArrayController. The problem I having is if the user performs a mouseDown in the TextField I need to update the selectionIndex of the array controller so the CollectionView will show the appropriate view as selected. Without this, the wrong view is selected. Here's a quick example:
>
> 1) click the add button twice to create two items in my collectionView. Items with index 0 and 1. Since item 1 was the last one created, it is selected (I have it showing a grey box).
> 2) click your mouse into the text field of the item at index 0 and start typing
>
> The user would expect item 0 to be the selected item but it's not. The array controller still thinks item 1 is selected. It needs to be told otherwise. If the user pressed the remove button item 1 would be removed.
>
> This is why I want to fire an action when the user inserts into the text field (just like I do when the user clicks the checkbox). I want to change the selectedObject in the array controller. The problem I'm having with subclassing the NSTextField is I can't figure out how to get the CollectionViewItem from the subclassed TextField.
Does -superview not do the trick?
Sometimes when I need to activate views on mouse clicks I use the following approach.
Subclass NSWindow and register the views I need click detection in with NSWindow - addClickView:
In NSWindow -sendEvent: I check to see if we have a hit and dispatch a message on the view
Might help you out.
@interface MGSClickWindow : NSWindow {
NSHashTable *_clickViews;
}
- (void)addClickView:(NSView *)aView;
@end
@implementation MGSClickWindow
/*
NSWindow designated initialiser
*/
- (id)initWithContentRect:(NSRect)contentRect styleMask:(NSUInteger)windowStyle backing:(NSBackingStoreType)bufferingType defer:(BOOL)deferCreation
{
if ((self = [super initWithContentRect:contentRect styleMask:windowStyle backing:bufferingType defer:deferCreation])) {
_clickViews = [NSHashTable hashTableWithWeakObjects];
}
return self;
}
/*
add a click view
click view must be a sub view of the NSWindow contentView
*/
- (void)addClickView:(NSView *)aView
{
if ([aView isDescendantOf:[self contentView]] && [aView respondsToSelector:@selector(subviewClicked:)]) {
// _clickViews will maintain a weak ref to aView so we don't need
// to remove it
[_clickViews addObject:aView];
}
}
/*
send event
This action method dispatches mouse and keyboard events sent to the window by the NSApplication object.
*/
- (void)sendEvent:(NSEvent *)event
{
// look for mouse down
if ([event type] == NSLeftMouseDown) {
// look for deepest subview
NSView *deepView = [[self contentView] hitTest:[event locationInWindow]];
if (deepView) {
for (NSView *aClickView in [_clickViews allObjects]) {
if ([deepView isDescendantOf:aClickView]) {
[(id)aClickView subviewClicked:deepView];
break;
}
}
}
}
[super sendEvent:event];
}
@end
> If I could I could then execute my method to update the ArrayController. I tried creating an IBOutlet to the CollectionView, the ArrayController and the CollectionViewItem but they all come back as nil. I think I read here that IBOutlets don't work in this instance. I also tried setting up my own Notification but the CollectionViewItem never receives it (other objects do).
>
> This is tricky, any help you may have would be appreciated.
>
> Brad
>
>
>
> On Jan 17, 2010, at 6:12 AM, Graham Cox wrote:
>
>>
>> On 17/01/2010, at 3:56 PM, Brad Stone wrote:
>>
>>> I was able to capture the mouseDown event in the field but only in a subclass which is causing me problems elsewhere.
>>
>>
>> Indeed, a mouse click is not the only reason a field might become focused - the user could tab into it as well.
>>
>> Overriding -becomeFirstResponder should do it.
>>
>> Taking a step back though, WHY do you need to get notified here? What are you trying to do? There might be a better way.
>>
>> --Graham
>>
>>
>>
>>
>
> _______________________________________________
>
> 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