Loading a nib file

Discussion in 'iOS Programming' started by xcodeNewbie, Jan 10, 2012.

  1. xcodeNewbie macrumors member

    Jul 1, 2011
    I'm trying to create a view controller and wire it up to a view by using a nib file. I know that to do this you can use:
    [[MyViewController alloc] initWithNibName:@"NibName" bundle:nil];
    However, specifying nil for the bundle will cause it to use the default bundle, but the nib I want to load is in a different directory. I don't really know anything about working with bundles, but I do have an NSString object that is the path for the specified nib. Thanks in advance!
  2. admanimal macrumors 68040

    Apr 22, 2005
    Regardless of where the nib is located on your hard drive, adding it to the Xcode project in the normal way will result in it being in the default location for nib files in the bundle, i.e. exactly where calling initWithNibName will expect it to be even if you specify a nil bundle.

    So unless you specified a custom build phase for your nib resources that caused them to end up in a nonstandard location, just use nil for the bundle.
  3. xcodeNewbie thread starter macrumors member

    Jul 1, 2011

    I don't think you understand... I'm writing an app for the iphone, just for fun, that can browse through the filesystem of the phone. If you look in another app's directory you can see its nib files there. I would like to be able to load any of those nibs.
  4. admanimal, Jan 10, 2012
    Last edited: Jan 10, 2012

    admanimal macrumors 68040

    Apr 22, 2005
    Then why didn't you say that in your original question?

    You'll have to load the file into an NSData object first, and then use UINib's nibWithData: method to create the nib object. Look at the UINib documentation to see how to instantiate the nib and get the actual UI objects from it.

    Unless you're working with a jailbroken phone, I'm not sure if the OS will even allow you to see or open files from anywhere you're not supposed to be looking, e.g. other app's bundles.
  5. jnoxx macrumors 65816


    Dec 29, 2010
    Aartselaar // Antwerp // Belgium
    You are saying that you want to browse through the file system, do you mean the file system of your app, or from the phone?
    Only works in your own app, not in the phone itself, or you will have trouble with Apple.. :)
  6. admanimal macrumors 68040

    Apr 22, 2005
    I've never bothered to try this- does the OS actually prevent you from opening files outside of your app's directories, or is it something that they catch during app review?
  7. PhoneyDeveloper macrumors 68040


    Sep 2, 2008
    Yes and no. Some parts of the file system don't exist as far as your app is concerned. Normal file system APIs will report errors or simply not report these parts of the file system if your code tries to access them. Other parts that are outside your app sandbox can be seen from your app but are illegal to read or access in any way according to the SDK agreement.

    You can run this method on a device and pass @"/" to it. You'll get a lot of stuff printed out but not everything will be printed out that exists.

    + (void)listFolderContents:(NSString*)inPath
    	printf("list of %s\n", [inPath fileSystemRepresentation]);
    	NSDirectoryEnumerator*	e = [[NSFileManager defaultManager] enumeratorAtPath:inPath];
    	for (NSString* file in e)
    		printf("%s\n", [file UTF8String]);

Share This Page