Resource Loading A resource is anything that isn't code. Resources include nib files, images, strings files, sound files, info.plist, and others. The basic guide is Loading Resources. Resources are loaded into the program in different ways. Since they are usually part of the bundle they are addressed simply by their file names. The application could acquire them elsewhere. Images Image files may be PNG, JPEG or other formats. The simplest way to load them is to put them in the bundle and use the file name: In the initialization of a view controller, for example, UIImage* theImage = [UIImage imageNamed:@"testImage.png"];and called from drawRect, [theImage drawAtPoint:CGPointMake(88, 300)]; Since code must run on different devices, there is a way to provide images in different resolutions and let the system choose which to use. The source code simply uses the file name. The bundle contains files based on that name but with modifications, Standard resolution: <ImageName><device_modifier>.<filename_extension> For example, if "testImage.png" is the basic name, you could provide, testImage.png (the default in case no other is offered) testImage~iPad.png (for the iPad) testImage@2x~iPhone.png (for the iPhone with Retina display) The system will select the best image for the device being used.
Nib files contain objects, usually interface elements like view controllers, views, controls, labels, etc. View controllers are often put into nib files. Whether initialized in code or from a nib, view controllers use "lazy loading" to manage their view. Nothing happens until the view is referenced. This diagram shows what happens. And this is how and when views are unloaded when memory is tight.
Strings files are used to localize the text in an application. They contain key-value pairs. They are stored in .lproj folders labeled with the linguistic group they represent. They are used by calling, NSString *NSLocalizedString(NSString *key, NSString *comment); This example comes from the International Mountains sample project. There is a folder titled "en.lproj" for English and one titled "fr.lproj" for French. In each is a file called Localizable.strings. In both are pairs of strings like this, "rootViewNavTitle" = "Mountains"; in the en.lproj file and "rootViewNavTitle" = "Montagnes"; in the fr.lproj file. In the viewDidLoad routine for the root view controller this line, self.title = NSLocalizedString(@"rootViewNavTitle",
@"Title used for the Navigation Controller for the root view");
will cause the system to check which language is chosen by the user (in Settings), then go to the corresponding .lproj folder to find the Localizable.strings file. It looks for a pair in which the first string is "rootViewNavTitle", the key, and returns the second string as the value to be used. Often the key and the value are the same in the language the programmer commonly uses. The second string is a comment that is unused by the system. In addition to a simple key-value lookup you can provide formatting elements and shape the result in various ways. Localization is a large topic. Apple's guide is Internationalization Programming Topics. The page was last updated
Friday, December 16, 2011 8:46 AM
|