Basic HTML tutorial

On 21/07/2010, in tech, video, by thomas

As an experiment I’ve made a basic HTML tutorial. Please watch it and tell me what you think.

Tagged with:  

iPhone delegates explained the easy way

On 26/06/2010, in iPhone, tech, by thomas

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:

Line for line:

@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.

Tagged with:  

Open PDFs in iBooks

On 24/06/2010, in iPhone, tech, by thomas

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:

Drag your PDFs here

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.

iBooks

Bus schedules in iBooks. Handy.

Tagged with: