Saturday 7 February 2009

Connecting up a label

Right to connect a label to a variable that we can access in code perform the following :

Chuck the following variable into your view controller .h file. See how you have to add it in two places, once as the variable within the brackets, and once as a property afterwards. The IBOutlet keyword is what makes this show up in interface builder so we can link it to the variable. Obviously you should call the label something more useful than just label which I've done here...

@interface MainViewController : UIViewController {

UILabel *label;

}


@property (nonatomic, retain) IBOutlet UILabel *label;


Then double click on the view.xib file to go into interface builder. Add the label to the View in interface builder. Then right click on the File's Owner icon and find the label underneath Outlets. To the right of this there is a little circle. If you click on it a + symbol appears. Drag this onto the label in the screen. The two should then be linked up nicely.

To change the text of the label in code just do something like the following :

label.text = @"Groove on people. Let love rule!";


And you're good to groove!

1 comment:

  1. Forgot to mention you need to chuck


    @synthesize label;

    in the .m file before the implementation.

    ReplyDelete