-3

I have 3 languages in tableview in ios. If user select any language, nib file change automaticaaly according to that language,How should i do it??

iPatel
  • 46,010
  • 16
  • 115
  • 137
sharmaravi
  • 137
  • 3
  • 13

3 Answers3

0

Create three UIViewController such like, VC1, VC2, VC3 and put .h file of all three (UIViewController) in your mainVC.m (Here mainVC is UITableView Containing UIViewController ) such like,

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

and use didSelectRowAtIndexPath method of 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];
    }

}
iPatel
  • 46,010
  • 16
  • 115
  • 137
0

Create a language class like this:

@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

Then have a function in the viewcontroller that you implement the tableview and you can have a dropdown menu using a pickerview:

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

Hope this helps...

lakshmen
  • 28,346
  • 66
  • 178
  • 276
-1

Try this.You can use three nib files as below for same ViewController or you can use three ViewController with three nibs.

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

}

Please dont forget to release the object if you are not using ARC.

Melbourne
  • 531
  • 3
  • 24