2013-02-18 63 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]無法找到您的第一個分配的控制器的路徑... 解決方案 - 發生一段時間,當您的項目突然停止工作,所以不用擔心 - 只需將項目轉移到其他路徑,或者只是更改項目所在的文件夾的名稱,並且它將開始正常工作....

它對我成功工作...