如何在更改本地化时快速重新加载故事板

时间:2016-04-24 09:04:23

标签: objective-c swift localization uistoryboard

我创建了一个项目并在其中使用本地化。

我在我的项目中使用此方法:language_manager并将语法更改为swift by self。

现在点击语言我的故事板没有改变: enter image description here

这是我的代码:

import UIKit

class ViewController: UIViewController {

    var data = [AnyObject]()

    @IBOutlet weak var label1 : UILabel!
    @IBOutlet weak var tables : UITableView!


    override func viewDidLoad() {
        super.viewDidLoad()
        self.data = LanguageManager.languageStrings()
        self.label1.text = NSLocalizedString("Happy New Year", comment: "")
        // Do any additional setup after loading the view, typically from a nib.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
}

extension ViewController : UITableViewDataSource,UITableViewDelegate {

    func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        return 1
    }
    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

        //print("\(ELanguage.Count)")
        return 4
    }
    func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
        return 50
    }
    func tableView(tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return 0.01
    }
    func tableView(tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
        return 0.01
    }
    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

        tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "cellIdentifier")
        let cell : UITableViewCell = tableView.dequeueReusableCellWithIdentifier("cellIdentifier")!
        cell.textLabel?.text = (self.data[indexPath.row] as! String)
        if indexPath.row == LanguageManager.currentLanguageIndex() {
            cell.accessoryType = .Checkmark
        } else {
            cell.accessoryType = .None
        }
        return cell

    }

    func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
        tableView.deselectRowAtIndexPath(indexPath, animated: true)

        LanguageManager.saveLanguageByIndex(indexPath.row)
        self.tables.reloadData()
        self.reloadRootViewController()
    }

    func reloadRootViewController() {

        let delegate = UIApplication.sharedApplication().delegate as! AppDelegate
        let storyString = "Main"
        let storyboard : UIStoryboard = UIStoryboard(name: storyString, bundle: nil)
        delegate.window?.rootViewController = storyboard.instantiateInitialViewController()
    }
}

1 个答案:

答案 0 :(得分:0)

尝试重新加载情节提要和根视图控制器:

- (void)reloadRootViewController
    {
        AppDelegate *delegate = [UIApplication sharedApplication].delegate;
        NSString *storyboardName = @"Main";
        UIStoryboard *storybaord = [UIStoryboard storyboardWithName:storyboardName bundle:nil];
        delegate.window.rootViewController = [storybaord instantiateInitialViewController];
}