views:

445

answers:

3

Hi all,

I am a complete newbie to mac/objective-c. My question is: I wonder if it's possible to bind a UILabel text to a variable, for not having to manually set the text when value change.

I explain it better, on mac os, when I open a new finder window and delete a file, then the global free space in the taskbar is changing. And that value also change in all the "finder" that are open.

How can I replicate such behaviour using objective-c, both on mac or iphone ? I was thinking about UILabel, but I couldn't find a way different from manually set each UILabel.

thanks Leonardo

A: 

It is very possible and there are 2 ways to do it.

Programatically by making a UILabel in the code and simply setting the myLabel.text.text of it

Though Interface Builder where you drag and drop the UILabel where you want it and have a property in the code to hook it up to and then use that property to set the myLabel.text on it.

James Raybould
+1  A: 

You will need to research the following:

  1. Notifications Notifications

and/or

  1. Key Value Coding KVC

Notifications will allow you to setup up automatic notifications of changes to let's say an object (e.g. variable) who's changes you want to be cascaded throughout your program. KVC allows you to hook up data to objects and may be helpful if you're using Core Data.

I'd start with notifications first.

Jordan
+1  A: 

The current version of iPhone OS (3.1) does not support bindings (such as you would find in desktop Cocoa). For the time being, you will need to write the controller glue manually to keep the UI in sync with your model.

Specifically, you would add an IBAction method in your controller, and connect the UILabel to call it when the contents changes.

This question has been covered before also:

On the Mac, you would use Key-Value Coding (KVC) and bind the label to an object controller in IB. The bindings documentation covers this in quite some detail:

gavinb