Titles, Strings, and Threads


I just got bit by a subtle Objective C bug. I’m writing a basic application that starts an asynchronous counter that updates an onscreen label. Common sense would suggest pseudo-code like this:

onButtonPress:
   if button title = "Start"
      set button title = "Stop"
      startBackgroundCounter
   else
      stopBackgroundCounter
      set button title = "Start"
end onButtonPress

The problem is this. The UIButton class in Cocoa has a read only currentTitle property. There’s no writing to this property however you can write to setTitleForState and pass a UIControlStateNormal enum. (Why do I hate enums?) So I’m thinking it’s enough to query the currentTitle property and then set the title for the normal state from “Start” to “Stop”, right? Wrong! Somehow the currentTitle property hangs onto the original value and instead I should be querying the titleForState: method instead! Can you tell I’m missing the JDK?

2 thoughts on “Titles, Strings, and Threads

  1. I don’t wanna pick on you (Astrid gave me your website months ago and now that you’re doing iPhone development I’m suddenly interested), but if you’re using the button’s title state to store the state of your counter, technically you’re breaking MVC practice. The isStarted/isStopped state of the counter is an attribute of your model, and you shouldn’t (I think the Apple people would say) be testing your UI’s current state in order to determine the state of your counter. Let your model dictate your button’s state FTW, not your button’s state dictate your button’s state.
    The hardest part of Cocoa dev is grokking exactly what the designers were thinking; remember that a lot of their design patterns were dreamed up by Smalltalk nerds in the 70’s, and it’s a very different way of approaching things than modern Java peeps do, at least given the Java implementation code I’ve read.

  2. Thanx Jamie! Keep the criticism coming, really. I appreciate every comment especially the critical ones. I know what I’m doing is completely against MVC and doggone dumb to say the least. I’m just trying to get comfortable with the big new old thing. (New cause it’s from Apple and it’s a language I’ve not touched before but old because it sorta reminds me of my C/RPG hacking days though not nearly as cryptic.) The purpose of my experiment was not to make an app exercising best practices, instead it was to figure out how to get HelloMyThreadedWorld running on the iPhone. I now know that calling the wait method on thread expects the delay time in seconds whereas in Java it’s in millis. (Imagine me waiting wondering why 1000 millis took so long in the iPhone Simulator!) I also now know that UI updates must be performed on the main thread and that you can jump back on this thread using a method on NSObject relatively easily. I got HelloMyThreadedWorld working and now I’m off to try HelloMyTDDWorld.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s