learningtech
learningtech

Reputation: 33705

how to update label/textfield of storyboard uiviewcontroller from controller.m?

I am new to iphone development, and i'm currently using xcode 4.2.

I drew a UIViewController into the storyboard. I put some labels into the UIViewController. It is associated to a controller.m file.

How do I, from the viewdidload function of the controller.m file, update the text in the label?

I don't know how to get the variable name or handle name of the elements in the uiviewcontroller on the storyboard.

Upvotes: 3

Views: 3205

Answers (1)

spring
spring

Reputation: 18497

If you are creating things through storyboard, the easiest way is to create an IBOutlet property for each UI element. This creates an instance property for the UIElement which you can then reference in your code - to set state, get values, etc.

You can do this through Control-click-drag on the UIElement to your UIViewController.h file (split view: RETURN-Command-Option: to display counterparts). XCode will pop up a dialog enabling you to name it (the same gesture will also enable you to create IBActions - the function that is called when the UIElement gets interacted with). You can also Control-click-drag on the left sidebar listing of the UIElements in your ViewController if that is easier.

There are YouTube videos that show it better than my weak explanation. I was skeptical @ storyboard/IB at first because I don't like UI magic and prefer to do things through code, or at least see the code that results from the magic, but it really does work pretty well and saves some of the tedium of UI coding.

One gotcha to be aware of: if you make an IBOutlet and then delete the UIElement, you will have code errors because of the errant reference. Those are easy to find and fix. The UIViewController object will also contain those references and will result in unpleasant crashes - so Control-click on the UIViewController object (or use the Inspector panel) and any element that has an orange triangle == broken.

Upvotes: 6

Related Questions