JAHelia
JAHelia

Reputation: 7932

cannot load image referenced from a nib in the bundle

I have a simple app, which contains two XIBs, each one is localized (for two languages en and ar for arabic) using XCode Add Localization feature in File Inspector pane.. I have placed two buttons in the root view controller xib to switch the app language and switch to the other localized XIB, the app works perfectly, except for one small issue: the Arabic XIB contains an image inside an imageView, and also the english XIB contains an image inside an imageView,

1) when I switch the language from English to Arabic, the Arabic XIB comes in place but without the image

2) and vice versa when I switch from Arabic To English

3) and even at app launch it does not display the image: XCode produces this error in these 3 cases in Console without crashing the app:

2011-12-27 12:48:30.412 LangSwitch[4903:f803] Could not load the "image1.png" image referenced from a nib in the bundle with identifier "(null)"

the didFinishLaunchingWithOptions method code is:

     [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(lang:) name:@"Lang" object:nil];
// Override point for customization after application launch.

// Set the navigation controller as the window's root view controller and display.
NSString* str = [[[NSUserDefaults standardUserDefaults]valueForKey:@"AppleLanguages"] objectAtIndex:0];
NSBundle* bnd = [NSBundle bundleWithPath:[[NSBundle mainBundle]pathForResource:str ofType:@"lproj"]];
    RootViewController* cont = [[RootViewController alloc]initWithNibName:@"RootViewController" bundle:bnd];
self.window.rootViewController = cont;
[self.window makeKeyAndVisible];

and the lang selector code is:

      - (void)lang:(NSNotification*)sender{
//NSLog(@"%@",[sender object]);
[[NSUserDefaults standardUserDefaults] setObject:[NSArray arrayWithObjects:[sender object],nil] forKey:@"AppleLanguages"];
[[NSUserDefaults standardUserDefaults]synchronize];

NSString* str = [[[NSUserDefaults standardUserDefaults]valueForKey:@"AppleLanguages"] objectAtIndex:0];
NSBundle* bnd = [NSBundle bundleWithPath:[[NSBundle mainBundle]pathForResource:str ofType:@"lproj"]];
RootViewController* cont = [[RootViewController alloc]initWithNibName:@"RootViewController" bundle:bnd];
self.window.rootViewController = cont;
[self.window makeKeyAndVisible];
}

and here is the sample app that contains what I'm describing above. if you guys could help solving this issue i'd be so grateful to you.

thank you so much in advance...

Upvotes: 1

Views: 4356

Answers (5)

Yunus Nedim Mehel
Yunus Nedim Mehel

Reputation: 12389

The problem is the xib file seeks the images in its own local bundle, but the images are not there and naturally you get an error. The solution is:

  • Remove the images from the xib file. That means you should only have empty imageviews, buttons without background images ..etc on your xib file.

  • Using the assistant window, connect all subviews to your header file and synthesize associated properties.

  • In your view did load method, manually set your images like:

    [_logoImageView setImage:[UIImage imageNamed:@"logo.png"]];
    
    [_button1 setBackgroundImage:[UIImage imageNamed:@"buttonImage.png"] forState:UIControlStateNormal];
    

Now your images are loaded from the main bundle, but your xib files are localized. The compiler will not give any error.

Upvotes: 2

Rohit Mandiwal
Rohit Mandiwal

Reputation: 10462

I simply browsed the XIB in the xml mode and added the relative folder path. For ex. I kept my images in img folder and image name is test.png. now I went in xml mode and changes test.png to img/test.png

It worked for me!

rohit

Upvotes: 0

Abizern
Abizern

Reputation: 150755

I don't think this can be done the way you are doing it, and even then, you are using an API that will get your app bounced from the App Store - Changing language on the fly, in running iOS, programmatically

But you could try localising the images (done through the property inspector).

Rather than try to load different nibs by changing the localization - you're going to have to change the nib manually. Rather than have the same nib file localized, you'll have to create two nib files, and load a language specific one based on the user's choice - you are also responsible for loading all the language localized resources yourself, which is not a small task.

The language choice really is better done at the level of the device by the user, rather than by yourself by trying to force a localization change - which as I've said is discouraged by Apple.

Upvotes: 1

Basheer
Basheer

Reputation: 1207

Check whether image "image1.png" is added to your project.

Also verify whether an IBOutlet is defined in the code and linked to the image or not.

Upvotes: 0

Mathieu Hausherr
Mathieu Hausherr

Reputation: 3485

Xib files load image from the main bundle and not from your custom language specific bundle.

You can't really change the language of a internationalized app programaticly. The user can change the language in his phone settings but you can't. If you want use more than one language in your app, put all the image resources in the main bundle (no internationalization) and set your own internationalization method (sample: rename pic.png to en-pic.png and ar-pic.png)

Upvotes: 1

Related Questions