天气 Swift 偏好
Weather Swift Preferences
我目前正在使用 xCode 开发应用程序,我是 iOS 的新手。我的天气格式目前正在向我的数据库提交 "Celcius" 和 "Fahrenheit",而不是发送 C 或 F。我尝试了几件事,但看起来它也将我的显示文本更改为 C 或 F。我希望将显示文本保留为 Fehrenheit & Celcius。这是我的代码:
import UIKit
import FirebaseAuth
import FirebaseDatabase
class PreferencesVC: UIViewController, UITextFieldDelegate,
UIPickerViewDataSource, UIPickerViewDelegate {
@IBOutlet weak var setTimeFormat: UITextField!
@IBOutlet weak var setUnitFormat: UITextField!
@IBOutlet weak var setLanguage: UITextField!
var timePicker = UIPickerView()
var unitPicker = UIPickerView()
var languagePicker = UIPickerView()
var timeData: [String] = [String]()
var unitData: [String] = [String]()
var langugageData: [String] = [String]()
var preferenceHandle: DatabaseHandle!
override func viewDidLoad() {
super.viewDidLoad()
self.hideKeyboardWhenTappedAround()
self.view.backgroundColor = UIColor(patternImage: UIImage(named: "background4.png")!)
guard Auth.auth().currentUser != nil else {
print("TEST: No current user")
performSegue(withIdentifier: "LoginVC", sender: nil)
return
}
self.setTimeFormat.delegate = self
self.setUnitFormat.delegate = self
self.setLanguage.delegate = self
timeData = ["12 Hour","24 Hour"]
unitData = ["Celcius", "Fahrenheit"]
langugageData = ["English","Francais"]
timePicker.dataSource = self
timePicker.delegate = self
unitPicker.dataSource = self
unitPicker.delegate = self
languagePicker.dataSource = self
languagePicker.delegate = self
setTimeFormat.inputView = timePicker
setUnitFormat.inputView = unitPicker
setLanguage.inputView = languagePicker
setTimeFormat.text = "12 Hour"
setUnitFormat.text = "Fahrenheit"
setLanguage.text = "English"
let userID = User.uid
self.preferenceHandle = DataService.instance.userRef.child(userID).child("UserPreferences").observe(DataEventType.value, with: { (snapshot) in
if let userPreference = snapshot.value as? Dictionary<String, AnyObject> {
if let timeFormat = userPreference["UserTimeFormat"] as? String {
User.timeFormat = timeFormat
switch(timeFormat){
case "12H":
self.setTimeFormat.text = "12 Hour"
break;
case "24H":
self.setTimeFormat.text = "24 hour"
break;
default:
self.setTimeFormat.text = "12 Hour"
break;
}
}
if let unitFormat = userPreference["UserTempFormat"] as? String {
User.unitFormat = unitFormat
switch(unitFormat){
case "F":
self.setUnitFormat.text = "Fahrenheit"
break;
case "C":
self.setUnitFormat.text = "Celcius"
break;
default:
self.setUnitFormat.text = "Fahrenheit"
break;
}
}
if let language = userPreference["UserLanguage"] as? String {
print("language = " , language)
User.language = language
switch(language){
case "English":
self.setLanguage.text = "English"
break;
case "Français":
self.setLanguage.text = "Français"
break;
default:
self.setLanguage.text = "English"
break;
}
}
}
})
// Do any additional setup after loading the view.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
@IBAction func cancelButtonPressed(_ sender: Any) {
print("THEY PRESSED THE CANCEL BUTTON MASTER")
DataService.instance.userRef.child(User.uid).child("UserPreferences").removeObserver(withHandle: preferenceHandle)
dismiss(animated: true, completion: nil)
}
@IBAction func submitButtonPressed(_ sender: Any) {
print("THEY PRESSED THE SUBMIT BUTTON MASTER")
var userPreferences: Dictionary<String,String> = [:]
if (self.setTimeFormat != nil) && setTimeFormat.text != "" {
if let timeFormat = setTimeFormat.text {
switch(timeFormat){
case "24 Hour":
userPreferences["UserTimeFormat"] = "24H"
break;
case "12 Hour":
userPreferences["UserTimeFormat"] = "12H"
break;
default:
userPreferences["UserTimeFormat"] = "12H"
break;
}
}
}
if (self.setUnitFormat != nil) && setUnitFormat.text != "" {
if let unitFormat = setUnitFormat.text {
switch(unitFormat){
case "Fahrenheit":
userPreferences["UserTempFormat"] = "F"
break;
case "Celcius":
userPreferences["UserTempFormat"] = "C"
break;
default:
userPreferences["UserTempFormat"] = "F"
break;
}
userPreferences["UserTempFormat"] = unitFormat
}
}
if (self.setLanguage != nil) && setLanguage.text != "" {
if let language = setLanguage.text {
userPreferences["UserLanguage"] = language
}
}
DataService.instance.userRef.child(User.uid).child("UserPreferences").updateChildValues(userPreferences)
DataService.instance.userRef.child(User.uid).child("UserPreferences").removeObserver(withHandle: preferenceHandle)
User.timeFormat = setTimeFormat.text!
User.unitFormat = setUnitFormat.text!
User.language = setLanguage.text!
dismiss(animated: true, completion: nil)
}
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
self.view.endEditing(true)
return false
}
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
var count = 0
switch(pickerView){
case timePicker:
count = timeData.count
break
case unitPicker:
count = unitData.count
break
case languagePicker:
count = langugageData.count
break
default:
count = 0
break
}
return count
}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
switch(pickerView){
case timePicker:
return timeData[row]
case unitPicker:
return unitData[row]
case languagePicker:
return langugageData[row]
default:
return timeData[row]
}
}
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
switch(pickerView){
case timePicker:
setTimeFormat.text = timeData[row]
break
case unitPicker:
setUnitFormat.text = unitData[row]
break
case languagePicker:
setLanguage.text = langugageData[row]
break
default:
setTimeFormat.text = timeData[row]
break
}
}
}
删除 submitButtonPressed
中的这一行,因为它会覆盖之前设置的
userPreferences["UserTempFormat"] = unitFormat
我目前正在使用 xCode 开发应用程序,我是 iOS 的新手。我的天气格式目前正在向我的数据库提交 "Celcius" 和 "Fahrenheit",而不是发送 C 或 F。我尝试了几件事,但看起来它也将我的显示文本更改为 C 或 F。我希望将显示文本保留为 Fehrenheit & Celcius。这是我的代码:
import UIKit
import FirebaseAuth
import FirebaseDatabase
class PreferencesVC: UIViewController, UITextFieldDelegate,
UIPickerViewDataSource, UIPickerViewDelegate {
@IBOutlet weak var setTimeFormat: UITextField!
@IBOutlet weak var setUnitFormat: UITextField!
@IBOutlet weak var setLanguage: UITextField!
var timePicker = UIPickerView()
var unitPicker = UIPickerView()
var languagePicker = UIPickerView()
var timeData: [String] = [String]()
var unitData: [String] = [String]()
var langugageData: [String] = [String]()
var preferenceHandle: DatabaseHandle!
override func viewDidLoad() {
super.viewDidLoad()
self.hideKeyboardWhenTappedAround()
self.view.backgroundColor = UIColor(patternImage: UIImage(named: "background4.png")!)
guard Auth.auth().currentUser != nil else {
print("TEST: No current user")
performSegue(withIdentifier: "LoginVC", sender: nil)
return
}
self.setTimeFormat.delegate = self
self.setUnitFormat.delegate = self
self.setLanguage.delegate = self
timeData = ["12 Hour","24 Hour"]
unitData = ["Celcius", "Fahrenheit"]
langugageData = ["English","Francais"]
timePicker.dataSource = self
timePicker.delegate = self
unitPicker.dataSource = self
unitPicker.delegate = self
languagePicker.dataSource = self
languagePicker.delegate = self
setTimeFormat.inputView = timePicker
setUnitFormat.inputView = unitPicker
setLanguage.inputView = languagePicker
setTimeFormat.text = "12 Hour"
setUnitFormat.text = "Fahrenheit"
setLanguage.text = "English"
let userID = User.uid
self.preferenceHandle = DataService.instance.userRef.child(userID).child("UserPreferences").observe(DataEventType.value, with: { (snapshot) in
if let userPreference = snapshot.value as? Dictionary<String, AnyObject> {
if let timeFormat = userPreference["UserTimeFormat"] as? String {
User.timeFormat = timeFormat
switch(timeFormat){
case "12H":
self.setTimeFormat.text = "12 Hour"
break;
case "24H":
self.setTimeFormat.text = "24 hour"
break;
default:
self.setTimeFormat.text = "12 Hour"
break;
}
}
if let unitFormat = userPreference["UserTempFormat"] as? String {
User.unitFormat = unitFormat
switch(unitFormat){
case "F":
self.setUnitFormat.text = "Fahrenheit"
break;
case "C":
self.setUnitFormat.text = "Celcius"
break;
default:
self.setUnitFormat.text = "Fahrenheit"
break;
}
}
if let language = userPreference["UserLanguage"] as? String {
print("language = " , language)
User.language = language
switch(language){
case "English":
self.setLanguage.text = "English"
break;
case "Français":
self.setLanguage.text = "Français"
break;
default:
self.setLanguage.text = "English"
break;
}
}
}
})
// Do any additional setup after loading the view.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
@IBAction func cancelButtonPressed(_ sender: Any) {
print("THEY PRESSED THE CANCEL BUTTON MASTER")
DataService.instance.userRef.child(User.uid).child("UserPreferences").removeObserver(withHandle: preferenceHandle)
dismiss(animated: true, completion: nil)
}
@IBAction func submitButtonPressed(_ sender: Any) {
print("THEY PRESSED THE SUBMIT BUTTON MASTER")
var userPreferences: Dictionary<String,String> = [:]
if (self.setTimeFormat != nil) && setTimeFormat.text != "" {
if let timeFormat = setTimeFormat.text {
switch(timeFormat){
case "24 Hour":
userPreferences["UserTimeFormat"] = "24H"
break;
case "12 Hour":
userPreferences["UserTimeFormat"] = "12H"
break;
default:
userPreferences["UserTimeFormat"] = "12H"
break;
}
}
}
if (self.setUnitFormat != nil) && setUnitFormat.text != "" {
if let unitFormat = setUnitFormat.text {
switch(unitFormat){
case "Fahrenheit":
userPreferences["UserTempFormat"] = "F"
break;
case "Celcius":
userPreferences["UserTempFormat"] = "C"
break;
default:
userPreferences["UserTempFormat"] = "F"
break;
}
userPreferences["UserTempFormat"] = unitFormat
}
}
if (self.setLanguage != nil) && setLanguage.text != "" {
if let language = setLanguage.text {
userPreferences["UserLanguage"] = language
}
}
DataService.instance.userRef.child(User.uid).child("UserPreferences").updateChildValues(userPreferences)
DataService.instance.userRef.child(User.uid).child("UserPreferences").removeObserver(withHandle: preferenceHandle)
User.timeFormat = setTimeFormat.text!
User.unitFormat = setUnitFormat.text!
User.language = setLanguage.text!
dismiss(animated: true, completion: nil)
}
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
self.view.endEditing(true)
return false
}
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
var count = 0
switch(pickerView){
case timePicker:
count = timeData.count
break
case unitPicker:
count = unitData.count
break
case languagePicker:
count = langugageData.count
break
default:
count = 0
break
}
return count
}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
switch(pickerView){
case timePicker:
return timeData[row]
case unitPicker:
return unitData[row]
case languagePicker:
return langugageData[row]
default:
return timeData[row]
}
}
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
switch(pickerView){
case timePicker:
setTimeFormat.text = timeData[row]
break
case unitPicker:
setUnitFormat.text = unitData[row]
break
case languagePicker:
setLanguage.text = langugageData[row]
break
default:
setTimeFormat.text = timeData[row]
break
}
}
}
删除 submitButtonPressed
中的这一行,因为它会覆盖之前设置的
userPreferences["UserTempFormat"] = unitFormat