2013-07-30 56 views
1

我的要求是從應用程序內切換本地化的XIB。通常,當您本地化XIB並從設置中更改語言時,特定語言本地化的XIB將自動加載。如何在應用程序中切換本地化的Nib?

我有XIBs像下面的圖像中:

enter image description here

我要加載的XIB當在一個應用程序中的按鈕抽頭選擇的特定語言。這種應用的源代碼可以在here找到。

我該怎麼做?

回答

1

我通過創建解決該問題的簡單方法見下文

-(void)changeControllersForLanguage:(NSString *)language{ 

    NSString *path = [[NSBundle mainBundle] pathForResource:language ofType:@"lproj"]; //Detecting specific language lproj folder 
    if (path) { 
     //Creating Local Bundle from the language specific bundle path 
     NSBundle *localeBundle = [NSBundle bundleWithPath:path]; 

     NSArray *controllers=self.tabBarController.viewControllers; 

     for(UIViewController *view in controllers){ 
      [localeBundle loadNibNamed:NSStringFromClass([view class]) owner:view options:nil]; 
     } 

    } 
} 

然後在按鈕自來水

- (IBAction)toggleLanguage:(id)sender { 
    UIButton *btn=(UIButton *)sender; 

    switch (btn.tag) { 
     case 1://French 
      [self changeControllersForLanguage:@"fr"]; 
      break; 
     case 2://English 
      [self changeControllersForLanguage:@"en"]; 
      break; 

     default: 
      break; 
    } 
} 

效果真的不錯調用該函數如下.. :)

+0

你可以通過將你的語言字符串放入一個數組'NSArray * languageCodes = @ [@「」,@「fr」,@「en」]',並調用'[self changeControllersForLanguage:languageCodes [btn.tag] ];' – dasblinkenlight