Category Archives: iOS development

Loading a view from a XIB

Ever since they where introduced I’ve been a huge fan of using storyboards in Xcode. Storyboards gives you a great overview of the flow in an app and in some cases even replaces the need of a separate mockup tool. But storyboards also have a huge disadvantage; By being stored in a single file it’s practically impossible to have several developers working on the same storyboard file at the same time. It is also rather cumbersome to reuse views and components in other projects when they are embedded in a storyboard file. All of this has made me go back to using single XIB-files again. XIB-files gives you a lot more flexibility and you can have XIB-files for all sorts of UI-components if you like.

But XIBs has their issues to. For example, I think it looks rather messy to load your UI components like this:

In order to getter a little bit cleaner code I packed this functionality up in a category like this:

Then by importing this category and make sure you’ve named your XIB files the same as your classes you can now get a little bit cleaner code.

Which way is up?

We have all been there sometime. You wake up and have absolutly no clue where you are or even which way is up. If you’re an app this happens all the time. But here’s a simple cure;

Properties in a Category

One of the many things I like with Objective-C is the possibility to extend any class without having to write a proper extension class that inherits from the original. The technique is called categories and what you is that you specify one or more new methods to be a part of a given class.

Below is an example of how to extend a standard NSString object with a new custom method that checks if a certain string is a part of the NSString.

To use the above category you simply import the NSString+Contains.h in your source code after that you can use the new method on all NSString objects. Great! But what if you also want to add a property? The category mechanism does not support properties but where there is a will, there is a way.

By declaring a property and then add your own getter and setter methods you can use the C-method objc_setAssociatedObject to connect and retrieve data to an object. Let me show you; In the following example I’ll create a category that will let you store any Objective-C object in a property called customData in a NSString object.

In many cases the best solution is to create a new class that extends another class to accomplish this, but for some situations this proves to be a highly efficient solution.

This is the code your are looking for

Since it’s obviously impossible for me to remember the best way of getting a random number in Objective-C I’m writing it down here, once and for all. This is a post to my future self.

- “John, this is the code you are looking for!”

Default Rotation

When creating an app that should start in landscape mode (mostly games) you should (read must!) support both landscape-left and landscape-right. This is actually a rather simple task, but one thing that has bugged me is that by default the app starts in landscape-left mode (home key to the left). I think it’s way better to start in landscape-right mode, not just because it feels better, most iPad cases (like the Smart Cover and Smart Case) is also designed to be used in landscape-right mode.

But how do you change the default direction? It turned out to be quite simple, you just switch the order of the value-strings for the UISupportedInterfaceOrientations key in the project-info.plist file.

Tracking Fingers

I decided to spend the evening writing on a new type of iPad game controlling class I came up with the other day. Even though I didn’t plan to use more than one touch point I started out playing with some multitouch code. I wanted to see if I could continuously track a bunch of touch points at once without mixing them up.

It all went all right at first but suddenly I noticed that some of the tracking nodes wasn’t removed even though the tracked touch were. After some tests I realised that I didn’t always got notified when a touch ended. We can’t have that!!! After even more tests I gave up trying to solve the matter using the standard callback methods touchesEnded and touchesCanceled, I just couldn’t trust them anymore. :-(

The solution was to keep track of the active UITouch objects and then check the phase property. Since the UITouch object is the same instance through out the complete touch-lifecycle I only had to store them in a separate dictionary and check them every now and then.

Want to see how I did it? Download the Xcode project below. :-)

FingerTracker.zip

New day, New design issues

Today I’ll spend most of my time finishing the transformation of an iPad app from iOS 6 to iOS 7. Hopefully without breaking it while introducing some more light and air.

Ever since the first beta of iOS 7 I have been struggling to decide whether I like the new look or not. I surly miss the skeuomorphism (I’ve actually been a sucker for real life looking UI ever since the first 3D-buttons in Windows) but I really enjoy the new fresh feeling, the new transitions and the blurred panes.

But maybe it’s like when trying to get the kids to like some new dish or vegetable, you have to taste it a few times (sometimes a lot of times) before you can truly enjoy the flavours?

Read what Apple says about the new iOS 7 Design