我做了一个应用程序,这个应用程序有几个功能。它有Table View
,Labels
,Buttons
等。我在名为Picker View
的类中创建了SettingsViewController
,这对于选择语言非常有用。我做了它并且它工作正常,但我不知道如何更改其他类的Table View
和Labels
的文本。我没有体验过上课。
这是我的班级SettingsViewController
:
import Foundation
import UIKit
class SettingsViewController: UIViewController, UIPickerViewDelegate, UIPickerViewDataSource {
var languages = ["English", "German", "French"]
var selectedLanguage = 0
@IBOutlet var languageLabel: UILabel!
@IBOutlet var topPartView: UIView!
@IBOutlet weak var languagePicker: UIPickerView!
@IBOutlet var SubmitLanguageButton: UIButton!
@IBAction func pickLanguageButton(sender: AnyObject) {
//Show Picker
topPartView.hidden = false
languagePicker.hidden = false
SubmitLanguageButton.hidden = false
}
@IBAction func submitLanguageButton(sender: AnyObject) {
//Hidde Picker
topPartView.hidden = true
languagePicker.hidden = true
SubmitLanguageButton.hidden = true
if (selectedLanguage == 0) {
languageLabel.text = "English"
}
else if (selectedLanguage == 1) {
languageLabel.text = "German"
}
else if (selectedLanguage == 2) {
languageLabel.text = "French"
}
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
topPartView.hidden = true
languagePicker.hidden = true
SubmitLanguageButton.hidden = true
languagePicker.delegate = self
languagePicker.dataSource = self
}
func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
return languages[row]
}
func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return languages.count
}
// returns the number of 'columns' to display.
public func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
selectedLanguage = row
}
}
另一个随机ViewController
Table View
和Label
:
import UIKit
class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
let foodList:[String] = ["Apple", "Bread", "Phineapple", "Water", "Other"]
@IBOutlet weak var myTableView: UITableView!
@IBOutlet var foodTitleLabel: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
foodTitleLabel.text = "Food"
myTableView.reloadData()
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
var returnValue = 0
return returnValue
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let myCell = tableView.dequeueReusableCellWithIdentifier("myCells", forIndexPath: indexPath)
myCell.textLabel!.text = foodList[indexPath.row]
myCell.textLabel!.numberOfLines = 0
return myCell
}
}
那么我将如何从另一个班级更改Labels
和Table View
,因为每个文字都必须使用SettingsViewController
中选择的语言?
例如:当用户选择英语时,所有ViewController
中的文本必须为英文。
当用户选择法语时,所有ViewController
中的文本必须是法语。
如果这个问题不明确,请通知我,我会尝试显示更多信息。谢谢你的贡献。
答案 0 :(得分:3)
我们可以用更少的代码以更清晰的方式实现这一目标:
enum names{
case English, German, French
func food() -> String {
switch self {
case English: return "food"
case German: return "Lebensmittel"
case French: return "Aliments"
}
}
func foodList() -> [] {
switch self {
case English: return ["Apple", "Bread", "Pineapple", "Water", "Other"]
case German: return [ "Apfel", "Brot", "Pineapple", "Wasser","Andere"]
case French: return ["Pomme", "Pain", "Ananas", "Eau", "Autre"]
}
}
}
import UIKit
var lang = names.English
class SettingsViewController: UIViewController, UIPickerViewDelegate, UIPickerViewDataSource {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
foodTitleLabel.text = lang.food()
foodList = lang.foodList()
答案 1 :(得分:2)
所以你可以通过不同的方法来解决这个问题,但是对于我的观点来说,最简单的方法是实现一个处理视图控制器中事件的协议,并在另一个协议中产生影响
protocol ChangeLanguagesProtocol
{
func returnNewLanguages(newlanguages:[String])
}
然后您需要在viewcontroller中实现协议及其方法,如
class SettingsViewController: UIViewController,
UIPickerViewDelegate, UIPickerViewDataSource , ChangeLanguagesProtocol {
var languages = ["English", "German", "French"]
func returnNewLanguages(newlanguages:[String]){
self.languages = newlanguages
}
然后在第二个ViewController类中,您需要拥有协议类型的var并在需要时触发其方法
//variable
var delegate:ChangeLanguagesProtocol!
//for example when clicking a button
@IBAction func SampleButtonPress(sender:UIButton){
let newlanguages:[String] = ["Spanish", "Chinesse", "German", "French", "Other"]
delegate.returnNewLanguages(newlanguages)
}
最后一步我们需要在我们要推送到所需的viewcontroller时分配委托变量,在viewcontrollers不是彼此相邻的情况下,你可以使用单例或共享实例来维护你的委托属性,直到你需要它,但它看起来像
// this should be in your first ViewController
@IBAction func gotoNextViewController(sender:UIButton){
let _localization = Localization(nibName: "Localization", bundle:nil)
_localization .delegate = self
self.navigationController?.pushViewController(_localization , animated: true)
}
答案 2 :(得分:2)
我找到了解决方案。首先是变量selectedLanguage
我将它从类SettingsViewController
中声明出来。
import UIKit
var selectedLanguage = 0 //<<< Here
class SettingsViewController: UIViewController, UIPickerViewDelegate, UIPickerViewDataSource {
var languages = ["English", "German", "French"]
之后我在方法viewDidLoad
中对每个ViewController
执行此操作(我只会显示一个ViewController
)。
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
if (selectedLanguage == 0) { //English
foodTitleLabel.text = "Food"
foodList = ["Apple", "Bread", "Pineapple", "Water", "Other"]
}
else if (selectedLanguage == 1) { //German
foodTitleLabel.text = "Lebensmittel"
foodList = [ "Apfel", "Brot", "Pineapple", "Wasser","Andere"]
}
if (selectedLanguage == 2) { //French
foodTitleLabel.text = "Aliments"
foodList = ["Pomme", "Pain", "Ananas", "Eau", "Autre"]
}
我希望这会对其他用户有所帮助,因为它对我来说非常有用。
答案 3 :(得分:1)
您正在尝试国际化您的应用。 Apple有一些内置的东西你应该看看:https://developer.apple.com/internationalization/
如果这不符合您的需求,为了回答您的问题,您的观点应该在加载(viewDidLoad)时或在它们出现之前设置文本(viewWillAppear),因为用户的语言选择可能存储在NSUserDefaults中。您的设置类不必强制在其他视图中进行任何更改,您的其他视图应在显示之前将自己的文本设置为正确的语言。
struct Localization {
static func stringsForScreen(screenName:String, forLanguage language:String) -> [String:String] {
//parse a file that has the strings for the given screen in the given language, and return it
}
}
class HomeViewController:UIViewController {
@IBOutlet var titleLabel:UILabel?
@IBOutlet var bodyLabel:UILabel?
override func viewDidLoad() { //or possibly viewDidAppear, depends on your needs
super.viewDidLoad()
let language = NSUserDefaults.standardUserDefaults().stringForKey("currentLanguage") ?? "English"
let textDict = Localization.stringsForScreen("HomeViewController" forLanguage:language)
titleLabel.text = textDict["titleLabelText"] ?? ""
bodyLabel.text = textDict["bodyLabelText"] ?? ""
}
}
class PickLanguageViewController:UIViewController, UIPickerViewDelegate {
var languages = ["English","French","Spanish"]
//Whatever method the picker uses
func pickerView(pickerView:UIPickerView didSelectRow row:Int inComponent component:Int) {
NSUserDefaults.standardUserDefaults().setObject(languages[row] forKey:"currentLanguage")
}
}
如何构建它的骨架示例。