Connect a UILabel in Interface Builder and XCode?

前端 未结 5 1262
梦如初夏
梦如初夏 2020-12-20 15:00

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

5条回答
  •  旧时难觅i
    2020-12-20 15:27

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

提交回复
热议问题