-3

我在 ios 的 tableview 中有 3 种语言。如果用户选择任何语言,nib文件会根据该语言自动更改,我该怎么做?

4

3 回答 3

0

创建三个UIViewController这样的 VC1、VC2、VC3 并将.h file所有三个(UIViewController)放在你的mainVC.m这里mainVCUITableViewContainingUIViewController)中,比如,

#import "VC1.h"
#import "VC2.h"
#import "VC3.h"

和使用didSelectRowAtIndexPath方法UITableView

#pragma mark - UITableView Delegate

- (void)tableView:(UITableView *)aTableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 0)
    {
        VC1 *ob1 = [[VC1 alloc] init];
        [self presentModalViewController:ob1 animated:YES];
    }
    if (indexPath.row == 1)
    {
        VC2 *ob2 = [[VC2 alloc] init];
        [self presentModalViewController:ob2 animated:YES];
    }
    if (indexPath.row == 2)
    {
        VC3 *ob3 = [[VC3 alloc] init];
        [self presentModalViewController:ob3 animated:YES];
    }

}
于 2013-02-27T06:37:29.357 回答
0

像这样创建一个语言类:

@implementation Language

static NSBundle *bundle = nil;

+(void)initialize {
 NSUserDefaults* defs = [NSUserDefaults standardUserDefaults];
 NSArray* languages = [defs objectForKey:@"AppleLanguages"];
 NSString *current = [[languages objectAtIndex:0] retain];
 [self setLanguage:current];

}

/*
  example calls:
  [Language setLanguage:@"it"];
  [Language setLanguage:@"de"];
*/
+(void)setLanguage:(NSString *)l {
 NSLog(@"preferredLang: %@", l);
 NSString *path = [[ NSBundle mainBundle ] pathForResource:l ofType:@"lproj" ];
 bundle = [[NSBundle bundleWithPath:path] retain];
}

+(NSString *)get:(NSString *)key alter:(NSString *)alternate {
 return [bundle localizedStringForKey:key value:alternate table:nil];
}

@end

然后在 viewcontroller 中有一个实现 tableview 的函数,你可以使用 pickerview 有一个下拉菜单:

- (NSInteger)selectedRowInComponent:(NSInteger)component{
    if (component == 0)
    {
      [Language setLanguage:@"de"];
    }
    if (component == 1)
    {
       [Language setLanguage:@"it"];
    }
} 

希望这可以帮助...

于 2013-02-27T07:07:16.510 回答
-1

试试这个。您可以为同一个 ViewController 使用如下三个 nib 文件,或者您可以将三个 ViewController 与三个 nib 一起使用。

- (void)tableView:(UITableView *)aTableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 0)
    {
        HomeViewController * objHome            =   [[HomeViewController alloc]initWithNibName:@"Write your 1 nib name here" bundle:nil];
    [self.navigationController pushViewController :objHome animated:NO];
    }
    if (indexPath.row == 1)
    {
        HomeViewController * objHome            =   [[HomeViewController alloc]initWithNibName:@"Write your 2 nib name here" bundle:nil];
    [self.navigationController pushViewController :objHome animated:NO];
    }
    if (indexPath.row == 2)
    {
       HomeViewController * objHome            =   [[HomeViewController alloc]initWithNibName:@"Write your 3 nib name here" bundle:nil];
    [self.navigationController pushViewController :objHome animated:NO];
    }

}

如果您不使用 ARC,请不要忘记释放对象。

于 2013-02-27T06:47:34.653 回答