Reputation: 533
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 i.e, "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.
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!):
import UIKit
class 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")
}
}
}
Upvotes: 0
Views: 500
Reputation: 1000
There's currently only one place where you're setting title
, and that's in viewDidLoad. Basically, you need to assign chosenLanguage
to title
every time chosenLanguage
changes.
The reason is that strings are value types in Swift. So when you assign a variable to title
, what you're actually doing is copying the current value of the variable into title
. Any changes to the variable afterward do not affect 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.)
Upvotes: 1