Create UIView from xib file

Discussion in 'iOS Programming' started by harry65, Jul 19, 2009.

  1. harry65 macrumors member

    Aug 26, 2008
    Hi All -

    I was wondering if there is any way to create a UIView from a XIB file? I'd like to set up my UIView in IB and then do something like:

    UIView *myView = [[UIView alloc] initWithNibName:@"myNib.xib" bundle:nil];
    I know UIViewController has the above method, but it does not look like UIView has this method. Is there no way to create a UIView from a XIB file? Am I trying to do something here that shouldn't/can't be done?

    If this is not the correct way, any suggestions?

  2. robbieduncan Moderator emeritus


    Jul 24, 2002
    Why not use a UIViewController that actually has the code to load/initialise an xib file (not a trivial matter) and then use the view property to get the view?
  3. Cocoaholic macrumors newbie

    Nov 27, 2008
    I think what you are looking for is:

    UIView *myView = [[[NSBundle mainBundle] loadNibNamed:@"myNib" owner:self options:nil] objectAtIndex:0];
  4. harry65 thread starter macrumors member

    Aug 26, 2008
    Thanks!! I finally got this to work (I had some other issues with what I was trying to do). This is exactly what I was looking for.

    @robbieduncan - I think that would have worked, though when I tried it I ran into the other problems that I had - I did not get to the point where a view would have showed up anyhow.

    Thanks for the feedback!

Share This Page