• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Drag 'n Drop 'n Delegation Dilemma
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Drag 'n Drop 'n Delegation Dilemma


  • Subject: Re: Drag 'n Drop 'n Delegation Dilemma
  • From: Demitri Muna <email@hidden>
  • Date: Tue, 18 Jun 2002 13:58:25 -0700

First, I'd like to thank Brian Webster and Matt Neuburg for their responses. Your comments were illuminating.

I was rather stubborn in trying to avoid subclassing, and in an attempt to learn more about the inner workings of Cocoa, I tried to solve this through some other means. I'm outlining my solution below for the curious, but also to make sure my solution is reasonable.

I began by creating a category for NSView which implements the methods for drag and drop.

#import <Foundation/Foundation.h>

@interface NSView (DragCategory)

- (unsigned int)draggingEntered:(id <NSDraggingInfo>)sender;
- (void)draggingExited:(id <NSDraggingInfo>)sender;
- (BOOL)prepareForDragOperation:(id <NSDraggingInfo>)sender;
- (BOOL)performDragOperation:(id <NSDraggingInfo>)sender;
- (void)concludeDragOperation:(id <NSDraggingInfo>)sender;

@end

For each of these methods, I post a notification. For example:

[nc postNotificationName:@"DNDDraggingEntered Notification" object:self];

Way back at my document controller, I registerForDraggedTypes: each view I want to drag into in awakeFromNib (e.g. NSButton, NSTextField, etc. - anything that is subclassed from NSView).

[searchButton registerForDraggedTypes:[NSArray arrayWithObject:NSFilenamesPboardType]];

I also create (in awakeFromNib) an observer for that same object, listening for the message that I send from the category:

[nc addObserver:self // set controller as observer
selector:@selector(handleDragIntoMyButton:)
name:@"DNDDraggingEntered Notification"
object:searchButton]; // only listen for my object

...and then of course I have a method called "handleDragIntoMyButton" that does what I need for that button alone.

I like this solution because I now have drag and drop capability for all subclasses of NSView without having to subclass anything, and I can place the custom code to handle it really anywhere I want to.

My question then is: is this the Cocoa Way, or is this fairly convoluted and more trouble than it's worth? :)

Thanks,

Demitri Muna
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.

  • Follow-Ups:
    • Re: Drag 'n Drop 'n Delegation Dilemma
      • From: Brian Webster <email@hidden>
  • Prev by Date: Re: protecting software & uniquely identifying the machine
  • Next by Date: Protecting Software w/ Serial # System
  • Previous by thread: Re: Drag 'n Drop 'n Delegation Dilemma
  • Next by thread: Re: Drag 'n Drop 'n Delegation Dilemma
  • Index(es):
    • Date
    • Thread