I’ve made my own custom photo gallery control, where the user can tap a photo to be taken to a larger version, or any other action you want.
I want this done so that the UIViewController holding this gallery contains the selectors, e.g
- (IBAction)photoThumbnailClicked(id)sender
{
// Enlarge the photo here
}
I’d want to somehow keep a reference of the view controller and selector in the photo gallery object, so when each photo thumbnail button is generated, the targets can be set like this:
[thumbnail addTarget:self.target
action:self.action
forControlEvents:UIControlEventTouchUpInside];
I’m currently keeping the references like this, although I’m not sure it’s the best way as I have reason to believe there may be memory issues:
PhotoGallery.h:
@interface PhotoGallery : UIScrollView
@property UIViewController *target;
@property SEL action;
- (void)setTarget:(UIViewController*)viewController
withAction:(SEL)action;
@end
setTarget method in PhotoGallery.m:
- (void)setTarget:(UIViewController*)target withAction:(SEL)action
{
self.target = target;
self.action = action;
}
Is this the best way of doing this? If not, what should I do instead?
Thanks in advance for any help!
I’ve assumed ARC
IF your view hierarchy looks like this:
Then you probably want your
targetto be a weak reference otherwise you will probably encounter a “retain cycle”. That issomeViewControllerholds on tophotoGalleryInstanceandphotoGalleryInstanceholds on tosomeViewControllerand neither is ever released.Otherwise, your code looks like the standard way of implementing this pattern.