4

I have created a UIView called ImageShowcase which has a UIScrollView and then i am adding UIButton inherited class call ImageShowcaseCell. Its working fine. One thing i am puzzled about is what is the best way to handle actions in a view controller where i have added ImageShowCase currently when i can handle UIControlEventTouchUpInside action in UIButton inherited class ImageShowcaseCell but i want to handle it in View Controller so that i can perform appropriate action.

What would be the best way to achieve this.

Mirza Bilal
  • 891
  • 11
  • 34

3 Answers3

4

In your custom ImageShowcaseCell class, you can send the action up the responder chain and allow the view controller to handle it:

[[UIApplication sharedApplication] sendAction:@selector(someMethodOnMyViewController:) to:nil from:self forEvent:nil];

Alternatively you could possibly also use:

  1. delegate pattern - set your view controller as a delegate for your ImageShowcaseCell.
  2. create a block based method in your custom class to execute the block after a touch.
Edwin Iskandar
  • 4,119
  • 1
  • 23
  • 24
1

A little late, but why not use the responder chain and add the action on your ImageShowCaseCell button? If you add it like so:

[imageShowCaseCellInstance setTarget:nil action:@selector(methodYouWantCalledOnYourViewController) forControlEvents:UIControlEventTouchUpInside

As long as your imageShowCaseCellInstance doesn't implement this method and your view controller does, the responder chain should pass this up (because nil was specified for target) to your view controller, as long as ImageShowCaseCell is in your view controller's hierarchy.

Edwin's answer (https://stackoverflow.com/a/13461504/482557) works, but by setting the target on the button you can be more explicit.

Community
  • 1
  • 1
Evan R
  • 875
  • 13
  • 27
0

Implemented it by using delegate. I already had one delegate in my view controller for ImageShowCase. So What i did i added another one in ImageShowcase for ImageShowcaseCell. Now i pass on the UIControlEventTouchUpInside event from ImageShowcaseCell to ImageShowcase, and ImageShowcase pass it to view Controller.

ImageShowcaseCell (TouchUpInside) ----> ImageShowcase ----> ViewController

Could not be cleaner. :)

Mirza Bilal
  • 891
  • 11
  • 34