简体   繁体   中英

Should the View or the Controller be responsible for drag processing?

As a rule, where does the primary responsibility for drag handling properly belong -- in the NSView or in the NSViewController.

The drag manipulation hooks are all contained in NSView, so it seems that NSView is the natural place. Moreover, the view will need to play some role, because the view knows where things are and so it's naturally the expert on where the drag is meant to wind up.

But drag are typically about inter-document or inter-process communication -- most particularly, about dragging files from Finder into a document. The drag is bound to result in changes or additions to the model. And that's the controller's job.

Finally, whether we're on the view or the controller, we're typically adding a bunch of methods to handle the drag-in-process and then to receive the data. Do people simple add the methods to the view or the controller, or do they forward them to a policy object?

The NSView is the "front line" for drag handling because, as you pointed out, "the view knows where things are and so it's naturally the expert on where the drag is meant to wind up." I can't speak for everyone, but the pattern I've seen is that, much like simple views like a button, when a view receives "user intent", it conveys that to a controller object.

Simple NSControls do this using the target/action paradigm, which can be hooked up in IB. One simple approach here might be to specify a target/action for your view, and when the drag operation has progressed to the point where it's time to trigger the action, the view can do so. Any state the view needs to convey to the controller can theoretically be accessed by virtue of the view being passed to the controller in the sender parameter of the action.

Naturally, the target/action paradigm is fairly limited, but the point here is that the view can track the drag until a "unit of user intent" has been established, it can be dispatched to controllers for handling. The exact mechanism will be dictated by the needs of each individual desired behavior.

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