Created
October 18, 2020 09:13
-
-
Save fredriccliver/d36b711e90a1864b6669879f1d98fe3d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import UIKit | |
class PreferencesViewController: UIViewController { | |
@IBOutlet var usernameLabel: UILabel! | |
@IBOutlet var profilePic: UIImageView! | |
@IBOutlet var profileBox: UIView! | |
@IBOutlet var signOutButton: UIButton! | |
@IBOutlet var learningLanguageTextField: UITextField! | |
@IBOutlet var translationTextField: UITextField! | |
var lastClickedTextField: UITextField? | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
createLocalePicker() | |
createToolbarForLocalePicker() | |
} | |
func createLocalePicker() { | |
let localePickerView = UIPickerView() | |
localePickerView.delegate = self | |
// add a one picker view to the both of locale text fields | |
learningLanguageTextField.inputView = localePickerView | |
translationTextField.inputView = localePickerView | |
} | |
func createToolbarForLocalePicker() { | |
let toolBar = UIToolbar() | |
toolBar.sizeToFit() | |
let doneButton = UIBarButtonItem(title: "Done", style: .done, target: self, action: #selector(dismissKeyboard)) | |
toolBar.setItems([doneButton], animated: false) | |
toolBar.isUserInteractionEnabled = true | |
learningLanguageTextField.inputAccessoryView = toolBar | |
translationTextField.inputAccessoryView = toolBar | |
} | |
@objc func dismissKeyboard() { | |
view.endEditing(true) | |
} | |
// Make set your list as you wish. | |
var localeList = Constants.LocaleList | |
// This Action had been connected with both of UITextFields. | |
@IBAction func textFieldBeginEditing(_ sender: UITextField) { | |
// Update last clicked text field. | |
lastClickedTextField = sender | |
print("lastClickedTextField is updated.") | |
} | |
} | |
extension PreferencesViewController: UIPickerViewDelegate, UIPickerViewDataSource{ | |
func numberOfComponents(in pickerView: UIPickerView) -> Int { | |
return 1 | |
} | |
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int { | |
return localeList.count | |
} | |
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? { | |
return localeList[row] | |
} | |
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) { | |
// upload selected item into the last clicked text field. | |
lastClickedTextField?.text = localeList[row] | |
} | |
func pickerView(_ pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusing view: UIView?) -> UIView { | |
var label: UILabel | |
if let view = view as? UILabel { | |
label = view | |
} else { | |
label = UILabel() | |
} | |
label.textAlignment = .center | |
label.text = localeList[row] | |
return label | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment