Silly question about alloc/init for UILabel

Discussion in 'iOS Programming' started by animefx, Jul 13, 2012.

  1. animefx macrumors regular

    Joined:
    May 10, 2005
    Location:
    Illinois
    #1
    I'm a beginner and know how to allocate memory for objects in most circumstances, still unclear on what situations I don't need to do it to begin with but anyway, I've seen lots of code like this...

    Code:
    UILabel *something = [[UILabel alloc] init];
    When I drag UILabel's with the Interface Builder in XCode I don't see anything in my code that generates this code. I am making the assumption that the memory is handled for me?

    Either way I'm curious as to why people would have to allocate memory for a label anyway. Does this only need to happen with a UILabel object is being created when the program is running? Is there a term for creating the UI controls when the program is running?

    Thanks
     
  2. xStep macrumors 68000

    Joined:
    Jan 28, 2003
    Location:
    Less lost in L.A.
    #2
    When you want to create a UILabel that isn't already existing, this is the way to do it. Reasons for doing that vary. The act in called instantiation. You are creating a new object based on a class.

    Often people use Interface Builder and create the labels there. In that case you declare a UILabel in your view class that the IB file is related to and link that to the IB UILabel interface object.

    What goes for this UI element goes for all of them.

    A simple answer regarding memory management. The memory is handled for you if you are using ARC. If you are using manual memory management, then it is up to you to release the objects you create. In your sample line because you used alloc, you'll need to use a release for that object at some point. In this case, likely right after adding it as a subview to some other view.
     

Share This Page