Setting up a custom delegate seems pretty straightforward. In this case we want the calling class to understand what the child is saying to it.
I’ve set up my PersonListTableViewController with a toolbar item that allows me to update my twitter status. It does so by popping up a new modal window where I type in my status and hit done to send it over to twitter. Here’s where I do that. In the main view controller I add the method that will trigger my modal window. didTapEditStatusButton:
{
StatusComposeViewController *statusComposeViewController = [[StatusComposeViewController alloc] init];
statusComposeViewController.delegate = self;
[self presentModalViewController:statusComposeViewController animated:YES];
[statusComposeViewController release];
}
The first thing that happens here is the modal window is instantiated.
Next I assign the delegate of the modal window to the main view controller. This is how we get the parent to listen to the child. Or, I guess this is where we tell the child who it’s daddy is.
When this is done all we need to do is present the controller, then release.
Because it is considered “Bad Form” for a modal window to dismiss itself, I’ll use delegation to let my parent view controller PersonListTableViewController know what the child view controller (my modal window) has done.
So the first thing I need to do is create my modal view. I’m going with the suggested StatusComposeViewController. Nothing new here, I’m subclassing UIViewController.
In the class header for my new view I need to add the delegation protocol. First thing I need to remember to do is declare the delegate protocol.
Then I create the protocol and give it a couple of optional methods:
@optional
-(void)statusViewControllerDidSetStatus:(StatusComposeViewController *)controller didSetStatus:(NSString *)text;
-(void)statusViewControllerDidCancel:(StatusComposeViewController *)controller;
@end
This essentially declares these methods for whatever controller you specify as your delegate.
Moving back into my delegate (PersonListTableViewController) I’m going to need to implement the methods I’ve defined in my modal view. For this post I’ll just do one, the statusViewControllerDidSetStatus method.
{
[TwitterHelper updateStatus:text forUsername:username withPassword:password];
[self didFinishWithModalViewController];
}
A simple method for updating twitter status. The TwitterHelper class is provided in the course files. didFinishWithModalViewController is a method I’m using for cleanup and dismissal of the modal window. (Dismiss using [self didFinishWithModalViewController]). Any view can only have one modal view up at any given time, so we don’t need to tell this method which modal view to dismiss… it will always be the current modal view.
What did I miss…
Ok in the modal view controller StatusComposeViewController I need to implement the methods I’ll use to hit the delegate methods to perform the actions that the modal view is intended for.
if([self.delegate respondsToSelector:@selector(statusViewControllerDidSetStatus:didSetStatus:)]){
[self.delegate statusViewControllerDidSetStatus:self didSetStatus:textField.text];
}
}
It is important to make sure you find out weather or not the delegate has implemented the method you’re going to try to call. This is done in the first line. Then we perform the operation.