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.
Many people are wondering how to get PDFs into iBooks. Some have even told me they didn’t think the iPhone got that functionality, since there is no book/pdf switch on top when you don’t have any PDFs in your library. So here is how to do it:
Method 1:
- Open a PDF in Safari.
- All the way up top there should now be a button: “Open in iBooks”. Press it.
Method 2:
- Open iTunes on your computer.
- Drag a PDF to your library (see picure). It should appear under Books.
- Connect your iPhone
- Make sure book sync is enabled (under the books tab), or check the box for the file there
- Sync
Now your PDF should open in iBooks.
PDFs reads a little differently in iBooks than elsewhere. It works better for books and papers in my opinion, but for other files like presentations and tables the normal view is equally good.
There you have it. I would guess the same also works on the iPad, although I can’t confirm that.
My new iPhone game Amazing Tic Tac Toe is doing great!
As simple as the game is, it is among the best in it’s category. Amazing Tic Tac Toe is a Tic Tac Toe game with both single and multiplayer, with 3 advanced AI opponents. And the best thing? It’s free!
The game is already a hit, 12 hours into it’s life. Over 5000 rounds have been played so far. And this is only the beginning. Updates will come, and they will come soon.
Can you beat it on hard? It is possible. Good luck!
Download
OK then, iPhone 4 it is. Almost everything I predicted hit a homerun:
- 5Mpx camera with 720P video and flash
- Front facing video camera for video chat
- Super-hardened glass front and back, 30 times harder than plastic
- Super high density 960 x 640 IPS display
- Stainless steel sides
- Gyroscope sensor
- Light sensor at the back
- Double microphones for noise cancellation
- A4 Chip
- 16 and 32GB editions (same as before)
OK, so I missed a few points, but most were spot on. Other news:
- iMovie for iPhone
- 1500 new APIs
Jobs also demonstrated much the same as the iPhone OS 4 event. Sort of ironic that iPhone OS 4 is now renamed iOS 4. Makes sense though, as it now runs in 3 iDevices.
You can read all the details and see pics over at Engadget. (Norwegian, VG nett)











