1. Welcome to the new MacRumors forums. See our announcement and read our FAQ

ios label.text null

Discussion in 'iPhone/iPad Programming' started by wordgeist, Sep 14, 2012.

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

    macrumors newbie

    wordgeist

    #1
    I'm setting a UILabel but is returning null.

    .h file:

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

    Code:
    @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. macrumors newbie

    #2
    Try to set [super viewDidLoad]; first.
     
  3. macrumors 68030

    PhoneyDeveloper

    #3
    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

    macrumors 6502a

    Duncan C

    #4
    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