Day 2 learning objective C
(for those that don't know I am taking a week to learn apple's Objective C. Bleep! BOX (my other iphone app) is written in C++ using LibNUI, so I haven't had to touch any Objective C so far)
Today I investigated Objective C++. Basically, if you rename your .M files to .MM then you can mix C++ code in with your Objective C code. Seems to work just fine. It's pretty common to interface with C/C++ libraries from Objective C (several example apps do). If I am ever forced to write iPhone apps that need to use standard GUI widgets then I would probably use regular Objective C for the interface and C++ for all the logic and data. Aside from being easier to work with (for me anyway), I think this will provide better separation between UI code and 'back end' code.. A lot of the Iphone examples I've seen so far have controllers for specific pages holding onto 'model' data. This can lead to architectural problems because there are a lot of cases where you want the two to be seperate and independent. I see the same issue with a lot of Flash code too, since the recommended way of doing things is always to subclass one of the Display classes. I usually find that using singletons for app-wide data is a better choice than attaching your data to a UI specific class.
I am also looking through the rest of the example apps, trying to learn more. A couple that caught my attention are the Wi-Tap app which shows how to do basic TCP connectivity (once again this references C++ code to get the job done), and also the LaunchMe app. Apparently it's possible to set up a custom URL type to launch an iphone app.. IE, have myappname://www.someurl.com/ open up your app. This would be cool for Bleep! BOX to allow people to browse a bunch of user submitted songs in safari and have the ability to click on one to open it up in the app. Seems very easy to implement.
AccelerometerGraph is the first example I've looked at so far that gives a good 'low level' look at how to make a UI component that draws itself. It also manually creates UI items which gives a better look at how things work internally. Digging in deeper..
More learnings:
- @selector is basically a delegate. Use it to call methods in response to actions
- In terms of reskinning existing objects, you can use Categories to override or add methods on objects without actually subclassing them. Though I'm a little unclear on how this works. Once I've made a category, does this apply to all instances of that object or can I choose whether I instantiate the the regular 'uncategorized' version of the object or the 'category' version?
- UIView - addSubView is how you add 'child' views.
- Good Info: http://www.iphoneexamples.com/, http://cocoadevcentral.com/d/learn_objectivec/
- This is interesting. A code only approach to iPhone ui - convert XIB to code: http://kosmaczewski.net/2009/03/17/nib2objc/
I'm starting to write my own Control from scratch. This is how I got started with LibNUI and should be a good way to get started with UIKit.