开发者

How to bind value of UILabel to an instance variable?

开发者 https://www.devze.com 2022-12-16 17:24 出处:网络
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, while not having to manually set the text when value change.

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, while not having to manually set the text when value change.

For example, on Mac OS, when I open a new Finder window and delete a file, then the global free space in the taskbar is changi开发者_如何学编程ng. And that value also change in all the "finder" that are open.

How can I replicate such behavior using Objective-c, either on Mac or for iPhone ? I was thinking about UILabel, but I couldn't find a way different from manually set each UILabel.

thanks Leonardo


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:

  • Bindings using Interface Builder (for iPhone apps)

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:

  • http://developer.apple.com/mac/library/documentation/Cocoa/Conceptual/CocoaBindings/CocoaBindings.html


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消