简体   繁体   中英

NSView Drag and Drop Data to Controller

I'm building an application where I have a subclass of NSView that I want to be a drag and drop area. I then want to pass the data from that operation to the view controller to process the information and do other tasks. I've found that I can't implement the drag and drop methods from the controller; I've had to do it from the view. This means that I have to find a good way to pass the information from the view back to the controller.

My first thought was to try to create delegate functions for the view and then try to implement drag and drop from the controller. This still seems like the best way, but I can't seem to make it work or find a tutorial where someone else did this. Since I am trying to pass data back, I don't think that notifications will work.

I haven't included any code because I am still at a conceptual stage for how to do this. I have been successful getting the information from the drag and drop information in the view -- the issue is where to go from there. If I just wanted to display information about the drag and drop in the view, or to perform another action with the view, then I should be set. My issue exists since I want to further process that information.

So, my question is, what is the best way to pass the data I get from the drag and drop even back to the controller. Should I continue trying to delegate the drag and drop methods so I can get the data directly in the controller? If so, does anyone have an example of where this has been done? I know this is a novice question, but any help would be greatly appreciated!

Answering my own question again...but this was novice.

I made my controller header a delegate of the NSCollectionView since that was the view type I am using.

@interface MainViewController : NSViewController <NSCollectionViewDelegate> 

In my controller awake from NIB, I set the view delegate to my controller...

- (void) awakeFromNib{
    [collectionView setDelegate: self];
}

And then implemented the drag and drop functions in the controller which allows me to accept the data and then make changes to both the view and the business logic as needed.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM