Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
808 views
in Technique[技术] by (71.8m points)

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...