Background:
I have an MVC based polaroid object. The model keeps the photo’s metadata, and image. The view displays the photo’s name and image (via UIView). The controller has access to the model and the view and coordinates the operations between them (eg. fade-in model photo on view)
My objective:
I want to implement a simple drag-and-drop function in my program so the user can drag a polarid’s view and viewcontroller into an album.
The problem:
I’m able to add the UIPanGestureRecognizer to my polaroid’s view. However, I’m unable to access the viewcontroller or model from the polaroidPanned method.
Adding the UIPanGestureRecognizer:
UIPanGestureRecognizer *panRecogniser = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(polaroidPanned:)];
[polaroidController.polaroidView addGestureRecognizer:panRecogniser];
Here’s the UIPanGestureRecognizer handler:
- (void) polaroidPanned: (UIPanGestureRecognizer *) panGesture
{
CGPoint touchLocation = [panGesture locationInView:self.view];
//While being dragged
if ([panGesture state] == UIGestureRecognizerStateBegan || [panGesture state] == UIGestureRecognizerStateChanged)
{
//update the album view to the touch location
[panGesture view].center = touchLocation;
}
else if ([panGesture state] == UIGestureRecognizerStateEnded && isOntopOfAlbum)
{
//HERE I WANT TO PASS THE VIEW CONTROLLER OF THE [panGesture view] to my album
[album addPolaroidViewController: ????]
}
}
I’m able to pass the view, but I cannot seem to be able to reference the view’s ViewController.
Does anyone know how I could accomplish this? Or work around it?
Assuming you cannot change the code of your PolaroidViewController or PolaroidView. You have alternatives. One of them is using associating objects.
In Objective-C you can insert runtime properties in objects. It’s comes with a couple of “gotcha” but if you have no control over the view and the controller this might help.
First import objc/runtime.h in your source
Declare a key used for getting your associate object
Then when you create your UIPanGestureRecognizer you associate an object like so:
To retrieve the associate object in your handler