Search code examples
iosuiviewuibuttonuicontrolevents

Passing action to one uiview to another in IOS


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.


Solution

  • 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.