0
votes

I am creating my first iPhone app in which the first screen displays a table view the user selects from. It works before localization using the method below.

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    

// Override point for customization after application launch.
navigationController = [[UINavigationController alloc] init];

[self.window addSubview:[self.navigationController view]];

if(self.selectCategoryViewController == nil)
{
    SelectCategoryViewController *viewTwo = [[SelectCategoryViewController alloc] initWithNibName:@"SelectCategoryViewController" bundle:[NSBundle mainBundle]];
    self.selectCategoryViewController = viewTwo;
    [viewTwo release];
}
[self.navigationController setNavigationBarHidden:YES];//this will hide the navigation bar
[self.navigationController pushViewController:self.selectCategoryViewController animated:YES];


//self.window.rootViewController = self.navigationController;
[self.window makeKeyAndVisible];

return YES;
}

After I localized it by adding the Japanese localization file and changing the above to this:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    

// Override point for customization after application launch.
navigationController = [[UINavigationController alloc] init];

[self.window addSubview:[self.navigationController view]];


if(self.selectCategoryViewController == nil)
{

    NSBundle *myLocalizedBundle=[NSBundle bundleWithPath:[NSString stringWithFormat:[[NSBundle mainBundle]bundlePath],"en.lproj"]];
    NSLog(@"the localized bundle is %@",myLocalizedBundle);
    SelectCategoryViewController *viewTwo=[[SelectCategoryViewController alloc] initWithNibName:@"SelectCategoryViewController" bundle:myLocalizedBundle];

    self.selectCategoryViewController = viewTwo;
    [viewTwo release];


}
[self.navigationController setNavigationBarHidden:YES];//this will hide the navigation bar
[self.navigationController pushViewController:self.selectCategoryViewController animated:YES];

[self.window makeKeyAndVisible];

return YES;
}

it crashes with the following error:

√sh.app> (loaded)
2013-02-18 18:04:35.196 PictureEnglish[5529:207] *** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[NSCFString substringFromIndex:]: Range or index out of bounds'
*** Call stack at first throw:
(
    0   CoreFoundation                      0x012775a9 __exceptionPreprocess + 185
    1   libobjc.A.dylib                     0x013cb313 objc_exception_throw + 44
    2   CoreFoundation                      0x0122fef8 +[NSException raise:format:arguments:] + 136
    3   CoreFoundation                      0x0122fe6a +[NSException raise:format:] + 58
    4   Foundation                          0x00033086 -[NSString substringFromIndex:] + 133
    5   PictureEnglish                      0x00008524 -[SelectCategoryViewController viewDidLoad] + 937
    6   UIKit                               0x00378089 -[UIViewController view] + 179
    7   UIKit                               0x00376482 -[UIViewController contentScrollView] + 42
    8   UIKit                               0x00386f25 -[UINavigationController _computeAndApplyScrollContentInsetDeltaForViewController:] + 48
    9   UIKit                               0x00385555 -[UINavigationController _layoutViewController:] + 43
    10  UIKit                               0x00386870 -[UINavigationController _startTransition:fromViewController:toViewController:] + 524
    11  UIKit                               0x0038132a -[UINavigationController _startDeferredTransitionIfNeeded] + 266
    12  UIKit                               0x0049c2e9 -[UILayoutContainerView layoutSubviews] + 226
    13  QuartzCore                          0x010a7a5a -[CALayer layoutSublayers] + 181
    14  QuartzCore                          0x010a9ddc CALayerLayoutIfNeeded + 220
    15  QuartzCore                          0x0104f0b4 _ZN2CA7Context18commit_transactionEPNS_11TransactionE + 310
    16  QuartzCore                          0x01050294 _ZN2CA11Transaction6commitEv + 292
    17  UIKit                               0x002ca9c9 -[UIApplication _reportAppLaunchFinished] + 39
    18  UIKit                               0x002cae83 -[UIApplication _runWithURL:payload:launchOrientation:statusBarStyle:statusBarHidden:] + 690
    19  UIKit                               0x002d5617 -[UIApplication handleEvent:withNewEvent:] + 1533
    20  UIKit                               0x002cdabf -[UIApplication sendEvent:] + 71
    21  UIKit                               0x002d2f2e _UIApplicationHandleEvent + 7576
    22  GraphicsServices                    0x01bcf992 PurpleEventCallback + 1550
    23  CoreFoundation                      0x01258944 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE1_PERFORM_FUNCTION__ + 52
    24  CoreFoundation                      0x011b8cf7 __CFRunLoopDoSource1 + 215
    25  CoreFoundation                      0x011b5f83 __CFRunLoopRun + 979
    26  CoreFoundation                      0x011b5840 CFRunLoopRunSpecific + 208
    27  CoreFoundation                      0x011b5761 CFRunLoopRunInMode + 97
    28  UIKit                               0x002ca7d2 -[UIApplication _run] + 623
    29  UIKit                               0x002d6c93 UIApplicationMain + 1160
    30  PictureEnglish                      0x00001cec main + 102
    31  PictureEnglish                      0x00001c7d start + 53
    32  ???                                 0x00000001 0x0 + 1
)
terminate called after throwing an instance of 'NSException'

Any suggestions on how I should call the SelectCategoryViewController for the selected language?

3
you have... somewhere in your code.. substringWithRange / NSRange? stackoverflow.com/a/6515336/1702413 – TonyMkenu
I am using it in the set up of the first view, but it does not crash before I localize the xib file (i.e. the nib). So I do not think that is the problem. It is as if it cannot find the localized file. Yet, when I have created a simple project with this same xib file but localized, it runs fine. BTW, I am using Xcode 3.2.6. – user1653733

3 Answers

0
votes

This line looks wrong to me:

NSBundle *myLocalizedBundle=[NSBundle bundleWithPath:[NSString stringWithFormat:[[NSBundle mainBundle]bundlePath],"en.lproj"]];

You probably meant something like this:

NSString* path= [[NSBundle mainBundle] pathForResource:@"en" ofType:@"lproj"];
NSBundle* bundle:myLocalizedBundle = [NSBundle bundleWithPath:path];
SelectCategoryViewController *viewTwo = [[SelectCategoryViewController alloc] initWithNibName:@"SelectCategoryViewController" bundle:myLocalizedBundle];

This is discussed more here: Manually loading a different localized nib in iOs

Note that this is only required if you want to do an in-app language selection. Localization should typically use the iOS device's selected language, in which case all this is much more straightforward (you simply let iOS pick the bundle for you).

0
votes

xcode 5: i had to clean build, clean build folder,delete derived data, reset simulator, close simulator, close Xcode.

Open project again and it's working.

0
votes

Problem - This is a problem of project path, I think [self.window makeWindowKeyVisible] can not be able to find a path to your first assigned controller... Solution - It happens some time, when your project suddenly stopped work, So no worries - just shift your project to other path, or just change the name of folder where actually your project resides and it will started working fine....

It works for me successfully...