Load UIView from NIB/XIB?

Discussion in 'iPhone/iPad Programming' started by Xino, Jan 13, 2009.

  1. macrumors member

    Joined:
    Sep 5, 2008
    #1
    I'm trying to load an UIView from a NIB/XIB file.

    The file is called 'Views.xib' and only contains a UIView (I didn't give it class name or something).

    In the UIViewController class called 'viewController.m' I use the following code to bind the view of the UIViewController to the UIView within the XIB file.

    Code:
    NSArray* nibViews =  [[NSBundle mainBundle] loadNibNamed:@"Views" owner:self options:nil];
    infoView = [nibViews objectAtIndex:0];
    [self.view addSubview:infoView];
    
    The application compiles without errors and/or warnings but the screen stays black... what am I doing wrong?

    Thanks for your help :)
     
  2. macrumors 6502a

    Joined:
    Oct 24, 2008
    Location:
    Cambridge
    #2
    loadNibNamed returns a boolean, not an array. Also AFAIK it only takes one argument. Clean and see what warnings you get.
     
  3. Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #3
    No, according to the Developer Documentation, NSBundle's "loadNibNamed: owner: options:" does return an array.

     
  4. macrumors 603

    jeremy.king

    Joined:
    Jul 23, 2002
    Location:
    Fuquay Varina, NC
    #4
    is a setNeedsDisplay: call needed?

    Also, you verified your view is at index 0?
     
  5. macrumors 68030

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #5
    The contents of the top level objects array isn't documented to be in any order. You should use an IBOutlet to get the reference to the view in the nib.

    Use the code from this thread:

    https://devforums.apple.com/message/12064
     
  6. macrumors 603

    jeremy.king

    Joined:
    Jul 23, 2002
    Location:
    Fuquay Varina, NC
    #6
    FWIW, I tried this last night. It worked fine. I created a view nib with a label in it...

    Where exactly did you place the above code? Are you sure its getting called? In my test, I simply put some code in the application delegate applicationDidFinishLaunching: method.

    Seems an outlet is a better idea :eek:
     

Share This Page