Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Connect a UILabel in Interface Builder and XCode?

I am trying to do something as simple as add a Label to a View in XCode and IB and I can't figure out how to do it. All the samples I find online are for older versions of IB so the directions aren't correct.

I have a label on my .xib file, in my Controller.h file I have an IBOutlet UILabel declared with a Property set up.

In my Controller.m file I synthesized that Property.

In Interface Builder, I cannot for the LIFE of me figure out how to associate my label in my code with the Label on the .xib. Whenever I try to drag the Connection to File's Owner, the only option that shows up is "View".

If I look at the Controller under the Library Window of Interface Builder, the Label shows up as a UILabel under Outlets. I am pretty sure that it used to be a type "id", but it automatically shows up as UILabel and if I try to add an "id" one, it doesn't work either.

Can someone point me to somewhere to explain this stupid thing? It should not be this difficult to make a label have text.

like image 323
James P. Wright Avatar asked Sep 30 '10 00:09

James P. Wright


4 Answers

Assuming your view is called ExampleView. Click on the file owner and then press ⌘+4. This will highlight the identity box. Make sure that the class name is the same as the name of your class.

Save and close Interface Builder and then go into Xcode and verify:

// ExampleViewController.h
#import <UIKit/UIKit.h>

@class ExampleViewController;
@interface ExampleViewController : UIViewController {

    IBOutlet UILabel *label;
}

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

@end

In your .m file:

// ExampleViewController.m
#import "ExampleViewController.h"

@implementation ExampleViewController

@synthesize label;

Then save the xcode files and open up your ExampleView. Drag a label onto the view. You are not supposed to connect that label to the Files owner.

INSTEAD YOU CLICK THE FILEOWNER. HIT ⌘+2 this will open the connections box. then you will see your outlet. Click and connect that to your label.

like image 174
Pavan Avatar answered Nov 01 '22 18:11

Pavan


Make sure your property line looks like this:

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

Leave (or set) the type of the label as UILabel in Interface Builder. If that doesn't work, try File -> Reload All Class Files in Interface Builder. Your code looks good, but CardNameLabel should start with a lower-case 'c'.

like image 23
James Huddleston Avatar answered Nov 01 '22 18:11

James Huddleston


Try this: click on the File's Owner icon to select it, and go to the Inspector's Identity tab (the 4th tab) and check the value of the Class setting. My guess is that's it's currently set to UIViewController.

Since the class that has the IBOutlet you declared is (or should be) a subclass of UIViewController, you'll need to change the class name to the name of your subclass (e.g., MyController, or whatever it's currently named).

like image 20
jlehr Avatar answered Nov 01 '22 20:11

jlehr


Here is how to connect a UILabel to your storyboard in swift:

  1. Click the icon icon in Xcode. If you are using an older version of Xcode, use the Venn Diagram at the top right of the window.

  2. Using the bar at the top, choose your storyboard on one half of the file viewer, and your view controller on the other side.

  3. Press control, click the UI Element you wish to create an IB Outlet/Action for, and drag it to the View Controller file: enter image description here
  4. Choose your preferences for your IBOutlet/Action: enter image description here

You have successfully linked your storyboard element to your code.

You can follow this tutorial to see a video on how to connect your storyboard element to your code.

like image 29
Rayaan Siddiqui Avatar answered Nov 01 '22 18:11

Rayaan Siddiqui