ios label.text null

Discussion in 'iOS Programming' started by wordgeist, Sep 14, 2012.

  1. wordgeist, Sep 14, 2012
    Last edited by a moderator: Sep 14, 2012

    wordgeist macrumors newbie


    Jun 15, 2009
    I'm setting a UILabel but is returning null.

    .h file:

    @property (retain,nonatomic) UILabel *label;
    .m file :

    @synthesize label;
    - (void)viewDidLoad
        self.label.text=@"some text";
     [super viewDidLoad];
    I check the value of label and is null. Can any body tell me why is null? Can anybody tellme what I'm doing wrong?
  2. veliborsantic74 macrumors newbie

    Sep 6, 2012
  3. PhoneyDeveloper macrumors 68040


    Sep 2, 2008
    It's probably nil because it isn't being set. How are you trying to set it?
  4. Duncan C, Sep 15, 2012
    Last edited: Sep 15, 2012

    Duncan C macrumors 6502a

    Duncan C

    Jan 21, 2008
    Northern Virginia
    Defining and synthesizing a property does not create the object the property points to. That just creates an instance variable and the getter/setter methods for the property. The instance variable starts off containing nil. You have to create an object and save it in the property or it will stay nil.

    Usually with views, you define your property or instance variable as an IBOutlet and connect it to an object in Interface Builder. Then when you load the XIB file or storyboard, the system instantiates a view and saves a pointer to it in your outlet.

    You can also create the view object manually and save a pointer to it in your property, but I don't recommend that. Creating views manually is more work, and it also makes for code that's harder to maintain. If you want to rearrange your view layout, you have to change code. Better to define your views in an XIB or storyboard.

Share This Page