Madefire Press

Blog

Toolboxes

Posted by Dan ,
general, hacking | Permalink | No Comments »

Craftsmen

If you look at any craftsman who has been working for a while, they gather a toolbox they take with them anywhere. For the physical world, it’s a real toolbox with all the dings, scratches, and well-used tools to go along with it. For software developers, it’s a virtual toolbox of scripts, code snippets, and useful notes they can use to improve their workflow and make getting to problem-solving easier.

At Madefire, we’re starting to build a toolbox of our own. As developers, we recognize the contribution to our increased productivity that open source software (OSS) provides us. While we’re usually focused on our mission, we do commit patches back to OSS projects we use such as AFNetworking.

Madefire’s Toolbox

In recognizing that we want to contribute back as much as possible, we’ve published our first bit of open source code in the Madefire toolbox repo. We intend for this to be a collection of anything we create that we find useful. We use a diverse set of languages and tools to create our Motion Books and readers, so this repository won’t be limited to just iOS or web tools; however our first submission is something for iOS.

UIStoryboard+DeviceNamedFiles

In the Cocoa Touch framework’s UIImage class, one can use the +imageNamed: method, which has some useful behavior that helps get the correct image based on device and screen scale. For example, calling [UIImage imageNamed:@"image"] will return image@2x~iphone.png for iPhone retina, and image~ipad.png for iPad non-retina devices. This is a huge time saver in the amount of code one needs to write to cover all devices you might run on.

For UIStoryboard files, the developer has to specify if they are for iPad or iPhone at creation. This leads to having two storyboard files for each interface idiom that you support. In your code you might have to select between the two, leading to something like this:


UIStoryboard *storyboard;
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
storyboard = [UIStoryboard storyboardWithName:@"iPadStoryboardFilename" bundle:nil];
} else {
storyboard = [UIStoryboard storyboardWithName:@"iPhoneStoryboardFilename" bundle:nil];
}

As practiced software developers know, that kind of code branching and repetition is something to avoid. That’s why we came up with the UIStoryboard+DeviceNamedFiles category. It consists of one method, +mf_deviceStoryboardWithName:bundle:. You can use it much like UIImage‘s +imageNamed: method:


UIStoryboard *storyboard = [UIStoryboard mf_deviceStoryboardWithName:@"StoryboardFilename" bundle:nil];

If you’ve created your storyboard files as StoryboardFilename~ipad.storyboard and StoryboardFilename~iphone.storyboard, then the +mf_deviceStoryboardWithName:bundle: call will return the correct one for each device. If you have a storyboard file without a device specifier (e.g., StoryboardFilename.storyboard) and no device specific file is found, it will return that. If it can’t find a storyboard file to return, nil is returned just like +storyboardWithName:bundle:.

Update: I have a bug report with Apple about including this functionality. You can see bug #12753707 at OpenRadar.

Leave a Reply

 

Get the RSS feed

Sign-up for our newsletter