Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 使用字符串变量设置导航栏标题_Ios_Swift_Variables_Navigationbar_Titlebar - Fatal编程技术网

Ios 使用字符串变量设置导航栏标题

Ios 使用字符串变量设置导航栏标题,ios,swift,variables,navigationbar,titlebar,Ios,Swift,Variables,Navigationbar,Titlebar,我将导航栏标题设置为: override func viewDidLoad() { super.viewDidLoad() languageMenu() let titleAttributes = [NSAttributedString.Key.font: UIFont(name: "AmericanTypewriter", size: 22)!] navigationController?.navigationBar.titleTextAttributes

我将导航栏标题设置为:

override func viewDidLoad() {
    super.viewDidLoad()

    languageMenu()

    let titleAttributes = [NSAttributedString.Key.font: UIFont(name: "AmericanTypewriter", size: 22)!]
    navigationController?.navigationBar.titleTextAttributes = titleAttributes
    title = "POLYGLOT"

    navigationItem.leftBarButtonItem = UIBarButtonItem(barButtonSystemItem: .add, target: self, action: #selector(addNewWord))
}
它可以很好地处理字符串文字,即“POLYGLOT”。 我想使用一个名为chosenLanguage的字符串变量,它根据用户在启动时选择的作为标题值的语言来更改值

尝试此操作时,导航栏标题区域中没有显示任何内容。在我的网络搜索中,我没有看到任何这样的例子

变量chosenLanguage是在languageMenu方法中设置的,如下所示(不知道前两行的格式发生了什么变化!):

导入UIKit

类ViewController:UIViewController、UITableViewDelegate、UITableViewDataSource{

@IBOutlet weak var languageButton: UIButton!
@IBOutlet weak var tableView: UITableView!

var words = [String]()
var chosenLanguage = String()
var languageSaved = String()
var toolbar = UIToolbar()

override func viewDidLoad() {
    super.viewDidLoad()

    languageMenu()

    let titleAttributes = [NSAttributedString.Key.font: UIFont(name: "AmericanTypewriter", size: 22)!]
    navigationController?.navigationBar.titleTextAttributes = titleAttributes
    title = chosenLanguage

    navigationItem.leftBarButtonItem = UIBarButtonItem(barButtonSystemItem: .add, target: self, action: #selector(addNewWord))
}

@objc func languageMenu(){

    let chooseLanguage = UIAlertController(title: "Vocabulary Tutor", message: "Choose a Language", preferredStyle: .actionSheet)

    let germanButton = UIAlertAction(title: "German", style: .default, handler: { (action) -> Void in
        self.chosenLanguage = "german"
        print("Choosen language is: \(self.chosenLanguage)")
        self.loadInitialValues()
    })

    let frenchButton = UIAlertAction(title: "French", style: .default, handler: { (action) -> Void in
        self.chosenLanguage = "french"
        print("Choosen language is: \(self.chosenLanguage)")
        self.loadInitialValues()
    })

    chooseLanguage.addAction(germanButton)
    chooseLanguage.addAction(frenchButton)

    self.navigationController!.present(chooseLanguage, animated: true, completion: nil)

    if let defaults = UserDefaults(suiteName: "group.co.uk.tirnaelectronics.polyglot") {
        if chosenLanguage == "german" {
            defaults.set(chosenLanguage, forKey: "languageChosen")
        } else {
            defaults.set(chosenLanguage, forKey: "languageChosen")
        }
    }
}

当前只有一个地方可以设置
title
,那就是viewDidLoad。基本上,每次
chosenLanguage
更改时,都需要将
chosenLanguage
指定给
title

原因是字符串是Swift中的值类型。因此,当您将变量分配给
title
时,实际操作是将变量的当前值复制到
title
中。之后对变量的任何更改都不会影响
title


(如果您是在Objective-C中编写的,而Objective-C没有值类型,那么同样的情况也会发生,因为
title
属性使用
copy
属性。)

向我们展示您的chosenLanguage变量以及您在何处设置该变量在设置变量之前放置断点可能有助于您调试此问题。很可能您没有正确设置该变量。loadInitialValues是什么?@El Tomato它加载从一开始就编程的英文和外文单词,而不是由用户添加的r、 确实是这样!我只是假设如果在将chosenLanguage分配给标题之前调用languageMenu,它会工作。我现在意识到,即使它工作一次,也不会在其他视图控制器之间来回移动时重新分配它。很高兴听到这一点。请确保在收到chan时将我的答案标记为已接受的答案总工程师。