0

I need all my view controllers to be able to show the Message compose view controller (MFMessageComposeViewController) and also handle MFMessageComposeViewControllerDelegate method to see whether a message was sent/cancelled/failed. So my idea was to subclass UIViewController impement these methods in this view controller. And then have all my view controllers that need to be able to send messages subclass the above view controller.

So like BaseMessageViewController : UIViewController

And then ViewController1 : BaseMessageViewController, ViewController2 : BaseMessageViewController and so on...

So when I was at it, I thought I would create a protocol like this:

@protocol MessageProcessing

@required

- (void)presentMessageCompose;
- (void)processMessageCancelled;
- (void)processMessageSent;
- (void)processMessageFailed;

@end

But I'm not sure if BaseMessageViewController should conform to this protocol or if my "concrete" view controllers should conform to it?

One thought was that if my BaseMessageViewController conforms to it then my concrete view controllers would automatically conform to it because of class inheritance? But I'm not seeing any warnings in my concrete view controllers that they are not implementing a required method.

Can someone please give me a helping hand here :)

2 Answers 2

1

If these are messages that BaseMessageViewController sends to self, there's no reason to define a protocol. Just declare the methods in BaseMessageViewController, and comment the declarations to describe when they are sent.

You will also need to provide implementations of the methods in BaseMessageViewController to suppress a compiler warning. If you want every subclass to implement the methods, you can define the methods in BaseMessageViewController using this pattern:

- (void)processMessageCancelled {
    [self doesNotRecognizeSelector:_cmd];
    abort();
}
Sign up to request clarification or add additional context in comments.

2 Comments

I'm looking for a way to override methods like the way you can do with Java.. you declare some methods in your superclass and then in your subclass you can have your own implementation (if you like) of the method that is declared in the superclass or just leave it to use the superclass method.
Also is it possible to add KVO to BaseMessageViewController so that all my view controllers will have KVO for a property set in BaseMessageViewController?
0

This is a classic example of the Abstract Base Class vs protocol question.

  • Use an Abstract Base Class (in Cocoa these are called Class Clusters) when you'd like to define a framework, with some common concerns encapsulated by the framework and some specific concerns to be handled by sub-classes. An example could be a message parsing framework.

  • Use a Protocol to define a common contract for classes that need their own object hierarchy. An example might be a media player, where 'play' and 'stop' are completely different depending on the type of media.

Alternatively, for something in-between, Justin Spahr-summers defines the 'concrete protocol' in libextobjc. . . https://github.com/jspahrsummers/libextobjc (similar to concrete interfaces in Java 8).

Comments

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.