Tag Archive for 'iPhone'

Testing View Controllers

I have been writing tests around my iPhone apps’ view controllers in order to follow the same TDD practices we use in other environments. Writing tests first has changed the way I structure my code in a couple of ways which I think offer immediate and emergent benefits for my applications. Most of an iPhone application’s business logic is implemented in its view controllers. Testing those controllers is therefore a priority if I want to have a well tested application.
Below are some examples of the sort of tests I have written for my view controllers using GTM, Hamcrest, and OCMock (our iPhone Unit Testing Toolkit). Hopefully this can serve as a starting point for the tests you could be writing for your own projects.
Continue reading ‘Testing View Controllers’

Multiple Environments in an iPhone Project

I’ve been following a Rails convention and defining multiple environments for my iPhone projects. This allows me to quickly switch application wide settings when running a test, development, or production version of the app.
Continue reading ‘Multiple Environments in an iPhone Project’

Resizing views around the keyboard

Most of my iPhone apps need to accept keyboard input somewhere and that means I need to reposition my UI elements such that the keyboard does not hide the field the user is typing in. The iPhone Application Programming Guide offers one example of Moving Content That Is Located Under The Keyboard but this is not necessarily the best solution.
Continue reading ‘Resizing views around the keyboard’

Loading UITableViewCells from a nib file

While working on iPhone applications I have found it useful to load as much of the UI layout and styling from nib files as possible. As a result I often want to load the cells for a table view from a nib file and created a factory class to handle this behavior every time I want to reuse it.
When I need to use a custom cell in a table view I add a cell factory as a property on the view’s controller and select a cell to use by setting that property when constructing the controller. This often allows me to reuse a single factory across multiple controllers so I don’t have to repeat myself by including duplicate references to the same cell nib in every controller.

Continue reading ‘Loading UITableViewCells from a nib file’

iPhone gesture recognition

I recently needed to support some basic gesture recognition in an iPhone app. As it turns out this is a little bit tricky to do when the touches occur within the same responder chain as a view which already responds to touches (like a UIScrollView or UIWebView which might need to scroll or zoom in addition to handling my custom gestures).
Continue reading ‘iPhone gesture recognition’

Resizing UILabel to fit text

UILabels are often the most convenient way to display simple text in an iPhone app however I often want a label to display variable text which may need to wrap to multiple lines. Simply increasing the height of the UILabel doesn’t help because a single line of text will be centered vertically within the label’s bounds. Instead I need to computer the size of the text I want to render and update the label’s frame’s height accordingly.

In most cases I want to adjust the size of not only a label but the view containing that label as well so I wrote a category to resize labels and their parent views.

Continue reading ‘Resizing UILabel to fit text’

Custom Xcode Templates for iPhone Development

Previously we discussed building anĀ iPhone Unit Testing Toolkit. I wanted to reuse that work without having to repeat those configuration steps every time I started a new Xcode project. Xcode provides a number of templates or starting iPhone applications so I made my own versions which include all of the unit testing tools we rely on.

Continue reading ‘Custom Xcode Templates for iPhone Development’