2013-02-18 97 views
0

我正在创建我的第一个iPhone应用程序,其中第一个屏幕显示用户从中选择的表格视图。它使用下面的方法在本地化之前工作。xib文件本地化后iPhone应用程序崩溃

- (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; 
} 

后,我加入了日语本地化文件,并改变上述这种本地化它:

- (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; 
} 

它与下面的错误崩溃:

 
√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' 

如何我任何建议应该为所选语言调用SelectCategoryViewController?

+0

你有...在你的代码中的某处.. substringWithRange/NSRange? http://stackoverflow.com/a/6515336/1702413 – TonyMkenu 2013-02-19 14:03:41

+0

我在第一个视图的设置中使用它,但在本地化xib文件(即笔尖)之前它不会崩溃。所以我不认为这是问题。就好像它找不到本地化文件。然而,当我用这个相同的xib文件创建了一个简单的项目,但本地化,它运行良好。顺便说一句,我使用Xcode 3.2.6。 – user1653733 2013-02-20 00:17:40

回答

0

这行看起来我错了:

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

你大概意思是这样的:

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

这更是这里讨论:Manually loading a different localized nib in iOs

注意,这仅在需要您想要进行应用内语言选择。本地化应该通常使用iOS设备的选定语言,在这种情况下,所有这些都更加直接(您只需让iOS为您选择软件包)。

+0

感谢您的建议,但仍然崩溃。另外,我同意你的看法,我应该能够本地化xib文件,并且ios应该只使用相应的nib文件,但是也会崩溃。 – user1653733 2013-02-20 00:19:26

+0

嘿,看着你的堆栈跟踪,你的SelectCategoryViewController的viewDidLoad在发生异常时执行,所以你的笔尖肯定会被加载。问题出在那里,这不是由于笔尖加载。 – Clafou 2013-02-20 00:50:05

+0

谢谢。我在想这个文件有问题,但绝对没有其他想法。参考点:当我右键点击xib文件并选择Get Info,然后点击“Make File Localizable”按钮,然后我可以点击“Add Localization”按钮添加另一种新语言。通过这样做,它将英语版本的xib保存在一个不是en.lproj文件夹的English.lproj文件夹中。我不认为命名惯例会导致这个问题,但我想我会在这里提及它。 – user1653733 2013-02-21 02:46:21

0

xcode 5: 我不得不清理build,clean build文件夹,删除派生数据,重置模拟器,关闭模拟器,关闭Xcode。

再次打开项目,它的工作。

0

问题 - 这是一个项目路径的问题,我认为[self.window makeWindowKeyVisible]无法找到您的第一个分配的控制器的路径... 解决方案 - 发生一段时间,当您的项目突然停止工作,所以不用担心 - 只需将项目转移到其他路径,或者只是更改项目所在的文件夹的名称,并且它将开始正常工作....

它对我成功工作...