简体   繁体   English

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

[英]Set navigation bar title using string variable

I'm setting my navigation bar title like this: 我将导航栏标题设置如下:

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))
}

Which works fine with string literals ie, "POLYGLOT". 可以与字符串文字(即“ POLYGLOT”)一起使用。 I want to use a string variable called chosenLanguage, which changes value depending on what language the user chooses at startup as the value for title. 我想使用一个名为selectedLanguage的字符串变量,该变量会根据用户在启动时选择哪种语言作为title的值来更改值。

I get nothing displayed in the nav bar title area when I try this. 尝试此操作时,导航栏标题区域中未显示任何内容。 I've not seen any examples of this in my search of the web. 我在网上搜索时没有看到任何此类示例。

The variable chosenLanguage is set in the languageMenu method like so (don't know what happened to formatting of first 2 lines!): 像下面这样在languageMenu方法中设置了selectedLanguage变量(不知道格式化前两行会发生什么!):

import UIKit 导入UIKit

class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource { 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")
        }
    }
}

There's currently only one place where you're setting title , and that's in viewDidLoad. 当前只有一个地方设置title ,它位于viewDidLoad中。 Basically, you need to assign chosenLanguage to title every time chosenLanguage changes. 基本上,您需要在每次chosenLanguage更改时将chosenLanguage分配给title

The reason is that strings are value types in Swift. 原因是字符串是Swift中的值类型。 So when you assign a variable to title , what you're actually doing is copying the current value of the variable into title . 因此,当您将一个变量分配给title ,实际上是将变量的当前值复制到title Any changes to the variable afterward do not affect title . 此后对变量的任何更改都不会影响title

(If you were writing this in Objective-C which doesn't have value types, the same thing would happen because the title property uses the copy attribute.) (如果您在没有值类型的Objective-C中编写此代码,则会发生相同的事情,因为title属性使用copy属性。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM