Create UIView from xib file

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

  1. harry65 macrumors member

    Joined:
    Aug 26, 2008
    #1
    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:

    Code:
    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?

    Thanks.
     
  2. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #2
    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

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

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

    Joined:
    Aug 26, 2008
    #4
    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