Many have problems understanding the concept of delegates in objective C. I think I’ve come up with an explanation that will clear this up for a lot of you. If you have never implemented a modal view, I suggest you do that before you read this.
First, the conceptual bit. A delegate in objective C is the same concept as in real life. You delegate a task to somebody else, who gives you the result when they are done. Or if there is no result, they just tell you they are finished.
So how does this work in code?
First you need to figure out what you want to use a delegate for. A common use is to close modal views, so I will use this in my example. The example is from one app I am currently developing.
When you want to close a modal view, they best way to do it is to let the parent view close it. The way to achieve this is to implement a delegate method. Let’s dive into it, shall we?
First, in your modal view, you have to implement a protocol. This is a description of what tasks you want to delegate to others. A protocol is defined in the header, like this:
@protocol HotelListViewControllerDelegate; // Tells the compiler: Hey! I’ve got a delegate protocol in this file!
id <HotelListViewControllerDelegate> delegate; // This is an instance variable where you save your delegate
@property (nonatomic, assign) id <HotelListViewControllerDelegate> delegate; // Create getters and setters for the delegate
@protocol HotelListViewControllerDelegate <NSObject> // Define your protocol
@required // This says that your delegate must implement these methods
- (void)hotelListViewControllerDone; // First delegate method. No arguments
- (void)hotelListViewControllerDoneWithHotel:(NSString *)hotel; // Second delegate methods. String as argument
@end // Declaration of protocole done
The only think you have to do in the modal views main file (.m) is to synthesize your delegate:
// Implements getter and setter for you (-(void)delegate and -(void)setDelegate).
That wasn’t too hard?
Implementing the delegate methods
First you must tell the view that initiated the modal view that it is the modal view’s delegate. In the header, you must show that you want to be the delegate for the modal view:
The only interesting part here is where you add <HotelListViewControllerDelegate>.
but how does the modal view know who to ask for help? It doesn’t yet. This we can fix, by adding one line to the method that creates the modal view in the main file (.m):
(I blacked out a bit hinting at what my app is about, this shouldn’t matter for your understanding of the code)
Here you set the modal view’s delegate to yourself.
And last but not least, implement the methods you described earlier (still in the parent view’s main file):
[self dismissModalViewControllerAnimated:YES]; // This dismisses the modal view
That’s it. If you followed this and implemented it in your own app, it should now work. Remember not to copy the names I use, but use names that fits with your app. If you have issues, comment below or mail me at help@thomassnielsen.com.
What is spotify?
Spotify is an on-demand streaming music service. It allows streaming of millions of songs to your computer at a low monthly fee.
I’ve been using Spotify Premium for a couple of weeks now. It’s a great service, delivering on demand music to my Mac and iPhone at great quality for 99kr (£9,99) a month. This service is currently only available in Europe, but they are planing on expanding.
I recommend Spotify to everybody who listen to music often. If you want an invite to the free version of Spotify (ad-supported), just send me an email. I have several invites left.
Tech specs:
- PC/Mac streams at 320kbps ogg/vorbis (premium only, free is 160kbps)
- iPhone streams at 160kbps
- OS required: Mac OS X 10.4 or later, Windows XP, or Linux with Wine
- Buffer HDD space recommended
Notes
Some users are having problem with 320kbps streams. Here is the fix.
Spotify for iPhone requires a premium account.
Props to Spotify for using open source software and open standards, and for giving us usable sound quality and Wine support!
I want spotify to add Metallica and Pink floyd to their collection!









