A couple of "firsts"

I've recently done a few things in my app development that I've never used before. I've done things in a similar way but I've never been satisfied with the resulting code. The apps worked but the code either wasn't elegant or tidy or I felt like I'd bodged it.

Anyway, I thought I'd share them just in case you feel the same about your code.

Auto Layout constraints in Interface Builder

After reading Ray Wenderlich's book iOS 6 by tutorials especially the Auto Layout chapters I decided to upgrade my current app to use Interface Builder auto layout constraints.

My usual practise is to use IB to throw the UI elements on the screen and then code the constraints myself. I always felt like it was a bit messy. I had an interface that didn't look like the app and code that seemingly did half a job.

Anyway, I followed all the rules from the excellent tutorials in the book and everything worked like a dream! I was animating, constraining and truncating like a demon! The end result was a massively reduced code base and a better looking interface file and everything worked perfectly.

UIPageViewController

Again, I've used this before but not for a very long time. The last time I used it was in the early days of my iOS dev and it was a complete mess.

I added a UIPageViewController subclass to my storyboard and with very little code got it to work. Taking a previously single view controller and converting it to a navigator with swipe gestures and spaces between each page.

Another really pleasing outcome from this with not too much effort.

This lead on to the next first...

NSObject IBOutlets

When configuring the UIPageViewController above I had to give it a datasource property. Much like a UITableView or UICollectionView it relies on this datasource object to tell it what to display on the screen.

Usually I would just make it the datasource of its self. However I've now started trying to practise clean coding and wanted to create a specific class to do the job.

I created the NSObject subclass and added a property to the UIPageViewController. However, with my new "get things working in IB" frame of mind I decided to use the NSObject outlet available in IB. I added the object, set the subclass, connected it to the property and finally set it as the datasource all from within IB.

Again, it worked perfectly first time. I've been wanting to use this for a while but never had the opportunity or the time (or knowledge) to fully understand how it works.

I will definitely be using this more in the future. I can see myself subscribing to Graham Lee's idea of creating separate datasource and delegate for table views and collection views.

I know from past experiments that OS X dev uses this principle quite a bit too. Who knows, maybe my next app will be a Mac app?

Leave a Reply

Your email address will not be published. Required fields are marked *