0

我正在创建我的第一个 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>(已加载)
2013-02-18 18:04:35.196 PictureEnglish [5529:207] *** 由于未捕获的异常“NSRangeException”而终止应用程序,原因:“***-[NSCFString substringFromIndex:]:范围或索引超出范围”
*** 第一次抛出调用堆栈:
(
    0 核心基础 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 基础 0x00033086 -[NSString substringFromIndex:] + 133
    5 图片英文 0x00008524 -[SelectCategoryViewController viewDidLoad] + 937
    6 UIKit 0x00378089 -[UIViewController 视图] + 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 石英核心 0x010a9ddc CALayerLayoutIfNeeded + 220
    15 石英核心 0x0104f0b4 _ZN2CA7Context18commit_transactionEPNS_11TransactionE + 310
    16 石英核心 0x01050294 _ZN2CA11Transaction6commitEv + 292
    17 UIKit 0x002ca9c9-[UIApplication_reportAppLaunchFinished] + 39
    18 UIKit 0x002cae83-[UIApplication_runWithURL:payload:launchOrientation:statusBarStyle:statusBarHidden:] + 690
    19 UIKit 0x002d5617-[UIApplication 句柄事件:withNewEvent:] + 1533
    20 UIKit 0x002cdabf -[UIApplication 发送事件:] + 71
    21 UIKit 0x002d2f2e _UIApplicationHandleEvent + 7576
    22 图形服务 0x01bcf992 PurpleEventCallback + 1550
    23 核心基础 0x01258944 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE1_PERFORM_FUNCTION__ + 52
    24 核心基础 0x011b8cf7 __CFRunLoopDoSource1 + 215
    25 核心基础 0x011b5f83 __CFRunLoopRun + 979
    26 核心基础 0x011b5840 CFRunLoopRunSpecific + 208
    27 核心基础 0x011b5761 CFRunLoopRunInMode + 97
    28 UIKit 0x002ca7d2-[UIApplication_run] + 623
    29 UIKit 0x002d6c93 UIApplicationMain + 1160
    30 图片英文 0x00001cec main + 102
    31 图片英文 0x00001c7d 开始 + 53
    32 ???0x00000001 0x0 + 1
)
在抛出“NSException”实例后调用终止

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

4

3 回答 3

0

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

它成功地为我工作......

于 2013-12-27T12:20:39.170 回答
0

xcode 5:我必须清理构建,清理构建文件夹,删除派生数据,重置模拟器,关闭模拟器,关闭 Xcode。

再次打开项目,它正在工作。

于 2013-09-24T15:01:14.617 回答
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 local nib in iOs

请注意,仅当您要进行应用内语言选择时才需要这样做。本地化通常应该使用 iOS 设备选择的语言,在这种情况下,这一切都更加简单(您只需让 iOS 为您选择捆绑包)。

于 2013-02-19T15:59:47.390 回答