IB call which method to initialize UIView?

Discussion in 'iOS Development' started by TechnoEagle, Aug 13, 2010.

  1. TechnoEagle

    TechnoEagle Member

    Joined:
    Jan 2, 2010
    Messages:
    169
    Likes Received:
    0
    Device:
    4G iPod touch
    I am currently writing my first ever iPhone app that I actually plan to publish to the App Store but have run into a minor issue. I wrote a class in Xcode that is a subclass of UIView. I added an instance of that class in Interface Builder.
    I need to do some custom initialization on the UIView subclass, but I need to know where to put the initialization code.

    If someone could please tell me which method Interface Builder uses to initialize a UIView I would really appreciate it!

    Thanks in advance!

    Please Register or Log in to view images

  2. Axis

    Axis Super Moderator Staff Member

    Joined:
    Dec 2, 2007
    Messages:
    6,288
    Likes Received:
    133
    Device:
    iPhone 4S (White)
    as with any subclass, override -init and call the superclass's implementation.
  3. TechnoEagle

    TechnoEagle Member

    Joined:
    Jan 2, 2010
    Messages:
    169
    Likes Received:
    0
    Device:
    4G iPod touch
    So I should just do:

    Code:
    - (id)init {
    /*
    Custom initialization
    */
    [super init];
    }
    Thanks for your help!
  4. Axis

    Axis Super Moderator Staff Member

    Joined:
    Dec 2, 2007
    Messages:
    6,288
    Likes Received:
    133
    Device:
    iPhone 4S (White)
    call super first, and remember to return self.
  5. MountainDew

    MountainDew New Member

    Joined:
    Feb 23, 2008
    Messages:
    194
    Likes Received:
    1
    Device:
    iPod touch
    This is usually the way of doing it:
    [objc]
    - (id)init {
    if (self = [super init]) {
    //customize initilization
    }
    return self;
    }
    [/objc]
  6. Pelaez-1

    Pelaez-1 New Member

    Joined:
    May 5, 2008
    Messages:
    822
    Likes Received:
    0
    Device:
    iPhone
    If I'm correct, the designated initializer for UIView is -(id)initWithFrame

    Please Register or Log in to view images

    CGRect)frame;
  7. SkylarEC

    SkylarEC Super Moderator Emeritus Staff Member

    Joined:
    Sep 19, 2007
    Messages:
    6,642
    Likes Received:
    129
    init, initWithFrame, *initWithCoder*, initWithZone, viewWillLoad, loadview, viewDidLoad, viewWillAppear, viewDidAppear, etc; or any one of an infinite number of methods that you can add to a view.

    How can we possibly tell you the best method to initialize anything. Take a look at your specific code and figure out where any initialization makes the most sense.
  8. TechnoEagle

    TechnoEagle Member

    Joined:
    Jan 2, 2010
    Messages:
    169
    Likes Received:
    0
    Device:
    4G iPod touch
    Thanks everyone!

    I think I understand now.

    Please Register or Log in to view images

  9. Pelaez-1

    Pelaez-1 New Member

    Joined:
    May 5, 2008
    Messages:
    822
    Likes Received:
    0
    Device:
    iPhone
    I'm almost sure even init calls initWithFrame:. Can't really make tests right now, though.
  10. Steaps

    Steaps New Member

    Joined:
    Oct 24, 2007
    Messages:
    5,074
    Likes Received:
    41
    Device:
    iPod touch
    With UIView, init does indeed call initWithFrame:

Share This Page