10

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.

James P. Wright
  • 8,991
  • 23
  • 79
  • 142
  • Please post the Controller.m and .h files – ennuikiller Sep 30 '10 at 00:45
  • PackViewController.h : http://pastebin.com/DesS8j50 PackViewController.m : http://pastebin.com/jasum3SB – James P. Wright Sep 30 '10 at 00:59
  • He doesnt have any problems declaring the iboutlet. He just needs to connect it properly. Hes trying to connect the label to the actual files owner icon which will just show VIEW. If you just look at the post i sent you, i wrote a bunch of code its the same as pselus, but thats just to MAKE SURE YOU WROTE THE CODE properly. then we go into interfacebuilder and i explain what you need to do in the post. check it below. thanks PK – Pavan Sep 30 '10 at 01:02

5 Answers5

8

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.

nicerobot
  • 9,145
  • 6
  • 42
  • 44
Pavan
  • 17,840
  • 8
  • 59
  • 100
  • Using this technic seemed like it was going to work, but now when I try to load that View I get a "SIGABRT" error with this message `this class is not key value coding-compliant for the key cardNameLabel`. – James P. Wright Sep 30 '10 at 01:07
  • i notice an error in your code... in your .H file.... youre supposed to add IBOUTLET after the property (retain, nonatomic) IBOUTLET uilabel *cardnamelabel. you just wronte property (retain, nonatomic) uilabel *cardNamelabel. which isnt an iboutlet – Pavan Sep 30 '10 at 01:10
  • I changed my code to use yours, except for the **, is that required and what in the world is it? – James P. Wright Sep 30 '10 at 01:11
  • sorry take those out. the next thing i want you to do is to change the [CardNameLabel setText:@"Test"]; in your .m file to a lowercase 'c' as you declared it with a lower case 'c'. also you wont be able to set the text until you connect his IBOutlet that you have pragmatically created with interface builder. so go in interface builder and connect the sucker up :D follow the guide i gave you – Pavan Sep 30 '10 at 01:13
  • I never put them in. As for the IBOutlet thing, I have it above that when I first declare the UILabel. From what I understood they were interchangeable. – James P. Wright Sep 30 '10 at 01:16
  • yep thats what i thought i tried that once without putting IBOutlet the second time but when i went into interface builder there seemed to be a problem so i just put IBOutlet in the property line and then there were no problems. have you connected the cardNameLabel outlet to your label on your view yet? – Pavan Sep 30 '10 at 01:18
  • OH RIGHT i see what you mean. That declaring you IBOutlet as a property is an option you dont have to do that. Yes you are right. The main thing to do though is to connect the iboutlet cardNameLabel in interface builder to the UILabel - that you dragged from the library onto your view. – Pavan Sep 30 '10 at 01:22
  • Moved the IBOutlet line, had no trouble hooking the label up in Interface Builder, but now when that View loads is when I get that error about `this class is not key value coding-compliant for the key cardNameLabel` and have no idea what that means. – James P. Wright Sep 30 '10 at 01:22
  • Other people have had similar probelms to this. Myself too. I had to set the Class Identity to the name of the class that it is loaded in. I thought that specifying the nib file was enough, but it looks like you need to also set the class for the view. if you go to your view and click on the files owner once and then hit CMD+4, in the class text box you have to write down the name of the class in your case PackViewController not just PackView it has to be PackViewController. – Pavan Sep 30 '10 at 01:26
  • Completely deleted the .xib file, made a new one, tested it and it worked with no Label. Then I added a label (and changed it from cardNameLabel to just cardName) and now I get the exact same error about key value coding-compliant. – James P. Wright Sep 30 '10 at 01:30
  • The class for File's Owner is already set to PackViewController. – James P. Wright Sep 30 '10 at 01:31
  • anyway you could send your project over? im assuming its just a plain project with a uilabel in it. – Pavan Sep 30 '10 at 01:35
  • The project is a default TabViewController project where I added a new View (that works perfectly until I add a Label). That's all there is to it. If I don't have the label, it works, if I add the label, it breaks. – James P. Wright Sep 30 '10 at 01:38
  • ok do you mind if i can create the same project as you and then send it over to yours? so this post can be solved? This way atleast you can carry on programming with your project for now and then in your spare time you can find out what the difference was between our projects and see what the minute error was. – Pavan Sep 30 '10 at 01:39
  • Feel free to do whatever you want. I just want to know WHY this is happening though. – James P. Wright Sep 30 '10 at 01:40
  • mate to be honest. All that code means is that the views are referenced in a way which are not matched to your code. – Pavan Sep 30 '10 at 01:47
3

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'.

James Huddleston
  • 8,410
  • 5
  • 34
  • 39
2

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).

jlehr
  • 15,557
  • 5
  • 43
  • 45
2

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.

Rayaan Siddiqui
  • 345
  • 2
  • 9
0

Assume that you have a viewController and a xib file which has a UILabel on this. The steps to connection the UiLabel (also the xib file) your viewController are:

1) In the header file, create UiLabel object and property for it

IBOutlet UILabel *label;

@Property (Nonatomic, retain) IBOutlet UILabel *label;

and synchthesize it in implement file

2) Open your xib file in Interface Builder

Double click on "File's Owner" then select the viewController in dropdownlist of pop-up windows to connect your xib file to controller

3) right-click on file's owner, on the pop-up dialog:

  • point and drag the plush (+) next to "View" and drop it on View row
  • point and drag the plush (+) next to "label" and drop it on the label on the view

=> now the label and the view on xib file is connected to you controller

Vikas S Singh
  • 1,748
  • 1
  • 14
  • 29
Son Nguyen
  • 3,481
  • 4
  • 33
  • 47