I'm trying to make an ios UI element where I can drag items on the screen onto a tableView and the tableView will add corresponding elements. I'm using the hitTest function to identify which view I'm dragging elements onto, and that's working fine. My problem is that hitTest gets me a pointer to a tableView, when I need the pointer to the tableView's controller so that I can call an "add new item" function. How do I go from a pointer to a view to a pointer to that view's controller?
相关问题
- Do the Java Integer and Double objects have unnece
- CALayer - backgroundColor flipped?
- Core Data lightweight migration crashes after App
- How can I implement password recovery in an iPhone
- State preservation and restoration strategies with
相关文章
- 现在使用swift开发ios应用好还是swift?
- UITableView dragging distance with UIRefreshContro
- TCC __TCCAccessRequest_block_invoke
- Where does a host app handle NSExtensionContext#co
- xcode 4 garbage collection removed?
- What exactly do pointers store? (C++)
- Unable to process app at this time due to a genera
- Swift - hide pickerView after value selected
The view controller is part of the responder chain. So from any view, just keep calling
nextResponder
until you reach an object that is a view controller.Another very cool trick when you need to call a method in your view controller from a view is to use a nil-targeted action. That's because a nil-targeted action walks up the responder chain looking for someone who handles the action. For example, here's code that sets a button's action to be nil-targeted:
If the view controller implements
doButton:
, it will be called when this button is tapped.You should not get the pointer to the
viewController
from a view. That will break the MVC as views should not know about its controller but controllers should know for which view(s) it is responsible.It can be done but bad practice obviously: How to get UIViewController of a UIView's superView in iPhone SDK?
You can usually use: tableView.dataSource or tableView.delegate
You shouldn't want to. Wanting to indicates that you are managing the drag in the wrong place.
Presumably you have a container view controller whose view has a number of subviews (each with a child view controller), and the table view is one of those.
In this situation, make the container view controller the target of the drag gesture attached to the items you want to drag around. When the drag gesture fires, the controller can reposition the view as appropriate and also has access to the table controller and its view (the drop target).
In this way you don't need to abuse any of the existing connections between views and controllers to try to repurpose them.
Well, you could use the dataSource and/or delegate and assume (or assume and verify) that they're the controller you want. A marginally better solution would be to subclass your UITableView and add a property that refers to the view controller.
As Anindya pointed out, you shouldn't be holding references to view controllers in views. A better approach entirely would be for the UI element that is handling the drags to have a delegate property (set to the table view's controller) with specific methods for dealing with the drag results. The view you're dragging things from shouldn't really know anything about the view you're dragging them to, handle that through the delegate methods.