UIButton无法正常工作或将无法识别的选择器发送到实例

时间:2016-08-28 08:09:07

标签: ios swift localization uibutton

我有一个奇怪的(至少对我来说)问题,因为我使用pod Localize_Swift来本地化我的应用程序。

我有两个问题我无法弄清楚,因为几乎所有关于本地化的文档都只涵盖了与本地化菜单或按钮相同的vc上的文本。就我而言,我已经设置了本地化的字符串,并且在整个应用程序中,它们都使用了.localize后缀。

第一期: 按钮要么不起作用。 print()函数永远不会被触发,或者如果它确实有效,看起来是随机的,我将无法识别的选择器发送到实例,该实例将func changeLanguage列为原因。我注释掉了函数中的所有代码,但仍然收到了错误。 我也删除了func并重新创建它,但仍然没有。

第二期: 如何使用字符串初始化整个应用程序的本地化?

SettingsViewController.swift

import UIKit
import Localize_Swift


class SettingsViewController: UITableViewController
{

    var availableLanguages = Localize.availableLanguages()
    var actionSheet: UIAlertController!

    @IBOutlet weak var switchlangButton: UIButton!
    @IBOutlet weak var flagImageView: UIImageView!


    override func viewDidLoad()
    {
        super.viewDidLoad()

        let backButton:UIBarButtonItem = UIBarButtonItem(title: "Back".localized, style: UIBarButtonItemStyle.Done, target: self, action: #selector(SettingsViewController.backBtnAction(_:)))

        self.navigationItem.leftBarButtonItem = backButton

        configureView()
        availableLanguages.removeAtIndex(0)
        print(availableLanguages)

    }


    override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(animated)
        NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(SettingsViewController.setTranslatedText), name: LCLLanguageChangeNotification, object: nil)
    }


    func setTranslatedText(){
        switchlangButton.setTitle("Switch Language".localized(), forState: UIControlState.Normal)
        flagImageView.image = UIImage(named: "flag-en".localized() + ".png")



    }

    override func viewWillDisappear(animated: Bool) {
        super.viewWillDisappear(animated)
        NSNotificationCenter.defaultCenter().removeObserver(self)
    }

    @IBAction func changeLanguage(sender: AnyObject) {
        print ("changeLanguageButton pressed")
        actionSheet = UIAlertController(title: nil, message: "Switch Language".localized(), preferredStyle: UIAlertControllerStyle.ActionSheet)
        for language in availableLanguages {
            let displayName = Localize.displayNameForLanguage(language)
            let languageAction = UIAlertAction(title: displayName, style: .Default, handler: {
                (alert: UIAlertAction!) -> Void in
                Localize.setCurrentLanguage(language)
            })
            actionSheet.addAction(languageAction)
        }
        let cancelAction = UIAlertAction(title: "Cancel".localized(), style: UIAlertActionStyle.Cancel, handler: {
            (alert: UIAlertAction) -> Void in
        })
        actionSheet.addAction(cancelAction)
        self.presentViewController(actionSheet, animated: true, completion: nil)
    }

    func configureView(){
        switchlangButton.setTitle("Switch Language".localized(), forState: UIControlState.Normal)
        flagImageView.image = UIImage(named: "flag-en".localized() + ".png")


    }


    override func didReceiveMemoryWarning()
    {


        super.didReceiveMemoryWarning()
    }


    @IBAction func backBtnAction(sender:UIBarButtonItem)
    {
        navigationController?.popViewControllerAnimated(true)
    }



}

“设置”场景的屏幕截图

Screenshot of the Settings scene

只是一个用于故障排除的FYI,图像也不起作用。

0 个答案:

没有答案