0

This has happened twice or thrice in one project. The requirement was to have a certain functionality in one viewController and then that feature was moved to another viewController.

I made a protocol that had declaration for those features but that doesn't help much. Every-time the requirement changes, I have to copy-paste all the methods implementations from one file to another and and then declaration of that protocol from one header-file to another.

Is there a way that I can have implementation in one place and then just somehow mix it with one class and then easily remove add it to another class like ruby mixins or for that matter traits in Scala ?

Amogh Talpallikar
  • 12,084
  • 13
  • 79
  • 135
  • 2
    possible duplicate of [Does Objective-C support traits/mixins?](http://stackoverflow.com/questions/5601126/does-objective-c-support-traits-mixins) – Michael Kohl Mar 27 '12 at 11:47

1 Answers1

1

You may not need a Ruby Mixin for your specific case. You could create a new class that inherits from UIViewController and use that as the base class for your view controller classes that need this protocol.

If you want that protocol available in all your view controllers then you could use a category to extend the base class.

If you need the same implementation of the protocol for classes that are not descended from UIViewController then you really do need something like a Ruby Mixin. In that case, see this question describing how to use message forwarding. And this question has some related information in the answers.

The appropriate choice depends on the set of classes that need your implementation. If it is needed in some but not all UIViewController classes, use a subclass. If it is needed in all UIViewController classes but no others, use a category. Otherwise, use message forwarding.

Community
  • 1
  • 1
Rik Renich
  • 774
  • 6
  • 12