仍然非常新颖,我在将更新的数据传递到pickerview时遇到了一些麻烦。我收到“索引超出范围”错误。我怀疑uipickerview看到的是空数组,即使数组正在更新。非常感谢您的帮助。
import UIKit
class ViewController: UIViewController, UITextFieldDelegate, UIPickerViewDelegate, UIPickerViewDataSource {
@IBOutlet weak var nameDisplay: UILabel!
@IBOutlet weak var ageDisplay: UILabel!
@IBOutlet weak var emailDisplay: UILabel!
@IBOutlet weak var pickerView: UIPickerView!
@IBOutlet weak var txtfirstName: UITextField!
@IBOutlet weak var txtAge: UITextField!
@IBOutlet weak var txtEmail: UITextField!
@IBOutlet weak var lblValidationMessage: UILabel!
var ages = [Int]()
var emailAddresses = [String]()
var firstNames = [String]()
override func viewDidLoad() {
super.viewDidLoad()
// lblValidationMessage.isHidden = true
// Do any additional setup after loading the view.
//links txtAge to the UITextField class, which gives access to func textfield
txtAge?.delegate = self
pickerView?.dataSource = self
pickerView?.delegate = self
}
// restricts the values possible to input in txtAge.text
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
let allowedCharacters = "1234567890"
let allowedCharcterSet = CharacterSet(charactersIn: allowedCharacters)
let typedCharcterSet = CharacterSet(charactersIn: string)
return allowedCharcterSet.isSuperset(of: typedCharcterSet)
}
@IBAction func addUserButton(_ sender: UIButton) {
//validation of data in the text boxes
lblValidationMessage.isHidden = true
if let firstName = txtfirstName.text, firstName == "" {
lblValidationMessage.isHidden = false
lblValidationMessage.text = "Enter Your Name"
return
}
if let age = txtAge.text, age == "" {
lblValidationMessage.isHidden = false
lblValidationMessage.text = "Please enter a numerical age"
return
}
if let email = txtEmail.text, email.isEmpty {
lblValidationMessage.isHidden = false
lblValidationMessage.text = "Please enter an email address"
return
}
//MARK: Adds entries to the 3 arrays
firstNames.append(txtfirstName.text!)
//Converts string to int, the age array requires INT
let age:Int! = Int(txtAge.text!)
ages.append(age)
emailAddresses.append(txtEmail.text!)
txtfirstName.text = ""
txtAge.text = ""
txtEmail.text = ""
//Brings focus back to First Name text field
txtfirstName.becomeFirstResponder()
}
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return firstNames.count
}
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
nameDisplay.text = firstNames[row]
}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
return firstNames[row]
}
}
更改数据后尝试致电reloadAllComponents()
它似乎正在使用静态var声明来更新数组,我不确定它在编程上是否正确。我确实认为使用结构体将是更好的方法。无论如何,感谢您对下一个错误的帮助!
// ViewController.swift
导入UIKit
类ViewController:UIViewController,UITextFieldDelegate,UIPickerViewDelegate,UIPickerViewDataSource {
@IBOutlet weak var nameDisplay: UILabel!
@IBOutlet weak var ageDisplay: UILabel!
@IBOutlet weak var emailDisplay: UILabel!
@IBOutlet weak var pickerView: UIPickerView!
@IBOutlet weak var txtfirstName: UITextField!
@IBOutlet weak var txtAge: UITextField!
@IBOutlet weak var txtEmail: UITextField!
@IBOutlet weak var lblValidationMessage: UILabel!
static var ages = [Int]()
static var emailAddresses = [String]()
static var firstNames = [String]()
override func viewDidLoad() {
super.viewDidLoad()
// lblValidationMessage.isHidden = true
// Do any additional setup after loading the view.
txtAge?.delegate = self
pickerView?.dataSource = self
pickerView?.delegate = self
}
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
let allowedCharacters = "1234567890"
let allowedCharcterSet = CharacterSet(charactersIn: allowedCharacters)
let typedCharcterSet = CharacterSet(charactersIn: string)
return allowedCharcterSet.isSuperset(of: typedCharcterSet)
}
@IBAction func addUserButton(_ sender: UIButton) {
lblValidationMessage.isHidden = true
if let firstName = txtfirstName.text, firstName == "" {
lblValidationMessage.isHidden = false
lblValidationMessage.text = "Enter Your Name"
return
}
if let age = txtAge.text, age == "" {
lblValidationMessage.isHidden = false
lblValidationMessage.text = "Please enter a numerical age"
return
}
if let email = txtEmail.text, email.isEmpty {
lblValidationMessage.isHidden = false
lblValidationMessage.text = "Please enter an email address"
return
}
updateArray()
}
func updateArray(){
ViewController.firstNames.append(txtfirstName.text!)
let age:Int! = Int(txtAge.text!)
ViewController.ages.append(age)
ViewController.emailAddresses.append(txtEmail.text!)
txtfirstName.text = ""
txtAge.text = ""
txtEmail.text = ""
txtfirstName.becomeFirstResponder()
//Checks to see data is being added to the arrays
print(ViewController.firstNames)
print(ViewController.ages)
print(ViewController.emailAddresses)
}
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return ViewController.firstNames.count
}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
return ViewController.firstNames[row]
}
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
nameDisplay.text = ViewController.firstNames[row]
let emailIndex: Int = row
emailDisplay.text = ViewController.emailAddresses[emailIndex]
let ageIndex: Int = row
ageDisplay.text! = String(ViewController.ages[ageIndex])
}
}