0

I've read a lot of articles to get some information about the whole delegate concept in objective c. At least I think I got the general idea of that. But I didn't found an answer for my question - or I just found it but didn't got it...

In my case I got a custom subview I'm adding programmatically on my UIViewController. I want to have an interaction from the custom UIView to call some methods of the controller. I think it would be better to have a delegate concept instead of making a reference to my UIViewController in the SubView, right? My question is, isn't it possible to do something with my UIScrollViewDelegate. My UIViewController is implementing some delegate methods because my SubView is a UIScrollView. So would it be possible to add a custom method which I can call with something like:

[self.delegate myCustomMethodOnUIViewController]

Or what would be the best way? Should I set up a protocol and add it as custom delegate to my UIViewController? Do I have to set up then a delegate property on my subview?

Thanks for help! Andy

NDY
  • 3,527
  • 4
  • 48
  • 63

1 Answers1

0

If you're adding the UIView as a subview to the UIViewController and you want to execute some methods from the superview in your subview there may be some mistake in your software design.

If there's a necessity to do it, then use the @class variable for circular inclusions and call your methods. To read more about @class directive.

http://developer.apple.com/library/ios/#documentation/cocoa/conceptual/objectivec/Chapters/ocDefiningClasses.html

More illustration can be read at the link: @class vs. #import

You can make it work with delegates and protocols, but make sure you handle memory properly within the same contexts to avoid leaks.

Community
  • 1
  • 1
Lalith B
  • 11,843
  • 6
  • 29
  • 47
  • Why is it a mistake in my software design. I've got a subview on my UIViewController in which I do select something my UIViewController should know about. I want to give this information back to him, isn't that a normal case for interaction between controller and view? – NDY Aug 17 '12 at 14:00