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?