Monday, 6 June 2011

Time to Give Xcode and Interface Builder one more Chance

Toady I 'ave mainly been making a minor breakthrough with my understanding of how to write simple iPad/iPhone apps. I wanted to be able to design a simple App that has Buttons and Labels using Interface Builder. Then get my Objective-C code in Xcode to respond to button clicks and update the Labels on the display. Not too much to ask?

I have spent some time reading a Book and browsing Apple Docs and watching uTube videos.

But still it was not obvious to me what had to be done in order to connect a button on the nice Interface Builder screen with a callback in my Xcode code. I had the same problem with Labels.

As you would expect, the answer turned out to be easy. Once you get into the Xcode/Interface Builder mindset.

I was expecting that on the Interface Builder Button screen I would click and get the Interface Builder to produce me some code to handle the button click. This is what I have seen in GUI builders before. This is NOT what happens. This is what took me by surprise.

Instead you go the the Interface Builder to design your screen by adding buttons and labels. This is good because you just concentrate on the design and layout of the screen. You do not worry about the code to make the application live.

When you are happy with your design, you go to the code and add Label objects and Button handlers to your code. In the code you do not say anything about how Buttons or Labels look on the screen (colour/size/position etc.).  This is also good as you just concentrate on behavior. You simply write callback handlers for the buttons and update the text on the Label objects.

The last bit of the trick is to make the Labels visible to the Interface Builder by using the IBOutlet specifier and also make the Button callbacks visible by use of the IBAction specifier. Once you have declared the IBOutlet andIBActions it is then easy to connect the Buttons and Labels in the Interface Builder to callbacks and objects in the Objective-C.

The exact mechanics of this are easy and, if I get time I will try to produce a uTube video showing you how. (I have watched several uTube videos that left me confused.)

I now have a simple App that is ready to try out on a real iPad.. Its now a question of time...

No comments:

Post a Comment