4

I have multiple classes (view controllers) that all conform to the same protocol and I would like them to implement exactly the same methods required by that protocol.

I cannot create a single base class with those method implemented and then make the view controllers inherit from that base class, because my view controllers all inherit from different classes anyway, and Objective-C doesn't allow multiple inheritance.

How can avoid pasting the same code (protocol method implementations) into multiple classes?

1

1 Answer 1

2

You could create a category on UIViewController that implements the protocol method.

Sign up to request clarification or add additional context in comments.

2 Comments

But would that boil down to creating separate categories for multiple view controllers and hence pasting the same code into each of those categories?
It depends. If your implementation of the protocol method uses unique properties of your custom ViewControllers, you won't have access to those properties in the category, so that wouldn't be an option. If the implementation doesn't use unique properties, just implement it in a category of UIViewController and make the category conform to the protocol, and you won't need to implement it multiple times.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.