views:

65

answers:

1

Here is my dilemma. I would like to have a text box and a button. The user types in text and then presses the button. Once the button is pressed, a text message window (using MFMessageComposeViewController) comes up. I don't know how to set this up. The problem is that the TextBox will require a delegate (UITextFieldDelegate) and the MFMessageComposeViewController will require an MFMessageComposeViewControllerDelegate. How can I have a .h file that declares a view that is more than one delegate?

I'm new to iPhone programming so any help on how to have an interface view that handles more than one delegate (so that I can have multiple types of controls in my view) would be really helpful! Thank you.

+1  A: 

A delegate does not need to be a view. Indeed, in most cases it probably shouldn't be. Often you will make a controller object the delegate, although this depends a lot on what you're doing.

The delegate protocols you need (MFMessageComposeViewControllerDelegate and UITextFieldDelegate) are quite distinct, so a single object can readily implement the methods of both without any confusion. But even if you are the same delegate type for several objects, the methods will be passed a pointer to the calling object so you can decide what to do case-by-case if necessary.

If you just mean how do you declare your class as implementing both protocols, you would do this:

@interface MyDelegate : NSObject <MFMessageComposeViewControllerDelegate, UITextFieldDelegate>
{
    ...
}

...although this presupposes that the protocols are formally required, which I don't think is the case here. In which case such a protocol list is unnecessary.

Otherwise, I probably am not understanding your question...


EDIT: OK, it seems like what you're looking for is how to link up the delegates at runtime. This varies according to the particular class, but for MFMessageComposeViewController you do this:

MFMessageComposeViewController* composer = ...;
id<MFMessageComposeViewControllerDelegate>* delegate = ...;
composer.messageComposeDelegate = delegate;

Easy, no? In this case the protocol is required, so you'd have to include it in the interface as described previously.

In general, if an object uses a delegate for anything, it will have a property or a method to allow you to set it, which you'll find in the documentation. Eg, in this case: Properties for MFMessageComposeViewController.

Note that delegate properties are conventionally weak references, so the objects in question need to be retained somewhere in your application.

walkytalky
Perhaps this can answer the question. How do you add a listener to an MFMessageComposeViewController? For example, for a textField, I found out that instead of using a delegate I can simply do something like[textField addTarget:self action:@selector(methodName) forControlEvents:UIControlEventEditingDidEndOnExit];What is the syntax for doing that with an MFMessageComposeViewController?
Robert Eisinger
By the way, thank you very much for your response. I didn't know you could use commas to have more than one delegate.
Robert Eisinger
@Robert Use the messageComposeDelegate property. See the update above.
walkytalky