Monday, June 24, 2013

The Scales have Fallen

A few days ago, I wrote:

One day, I will understand the relationship between labels and bindings in Xcode, and that day shall be as if the scales have fallen from my eyes. But that day does not seem to be today. #xcode app.net

I’ve been trying to teach myself how to program in Objective-C by rewriting my intro to computer science programs. A few weeks ago, I had written a basic change making program. It worked, but it put its output in the console log. I’m trying to learn how to write apps for the Mac, and eventually iOS, so I wanted to output everything to a window. I fiddled for a while and created a slider to enter the price, and some labels to output the change.

The slider worked fine, but I could not for the life of me get the labels to change when the variables I had bound to them changed. I was going around and around on this, trying different bindings, different code in the program, ctrl-clicking and dragging every property I could think of, but nothing changed. I could see in the console that the variables where changing, but the window stubbornly refused to update.

Then today I found this:

That you are changing the value using an accessor method, or using a key-value-coding compliant method. Changing the value of an instance variable directly does not provide key-value observing change notifications. Apple

I had been setting variables directly:

quarterValue = [actionChangeMaker numQuarter];

When I should have been setting them using the accessor methods:

[self setQuarterValue:[NSString stringWithFormat:@"%@",[actionChangeMaker numQuarter]]];

Once I did that, the labels started behaving, and program operated the way it should. And once I understood what it was doing, I was able to add in a new label that reflected a different variable, and it worked right away.

So, the scales have fallen from my eyes. On to more code!