Transferring actions from one uiview to another in iOS

I created a UIView called ImageShowcase that has a UIScrollView , and then I add a UIButton inherited call to the ImageShowcaseCell class. His work is wonderful. One thing I'm puzzled about is the best way to handle actions in the view controller, where I added ImageShowcase at the moment when I can handle the UIControlEventTouchUpInside action in the UIButton inherited ImageShowcaseCell class, but I want to process it in the View Controller so that I can perform the appropriate action.

What would be the best way to achieve this.

+4
source share
3 answers

In your custom ImageShowcaseCell class, you can submit an action along the responder chain and allow the view handler:

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

Alternatively you can also use:

  • delegate template - set the view controller as a delegate for your ImageShowcaseCell.
  • create a block based method in your custom class to execute the block after clicking.
+4
source

A bit late, but why not use the responder chain and add an action to the ImageShowCaseCell button? If you add it like this:

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

Until your imageShowCaseCellInstance implements this method and your view controller does not execute, the responder chain should pass this (because nil was specified for target ) to your view controller if ImageShowCaseCell is in your view of the dispatcher hierarchy.

Edwin's answer ( fooobar.com/questions/1447056 / ... ) works, but by setting the target on the button, you can be more explicit.

+1
source

Implemented with the help of a delegate. I already had one delegate in the view controller for ImageShowCase . So what I did was add another to ImageShowCase for ImageShowcaseCell . Now I am UIControlEventTouchUpInside event from ImageShowcaseCell to ImageShowcase, and ImageShowCase passes it to the view controller .

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

Could not be cleaner. :)

0
source

Source: https://habr.com/ru/post/1447056/


All Articles