0
我正在嘗試創建一個使用2個文本字段和1個選取器視圖的應用程序。一旦用戶點擊第一個文本字段,應該彈出選擇器。在用戶從菜單中進行選擇並點擊第二個文本字段後,選擇器會再次彈出,但菜單依賴於第一個文本字段中的文本(上一個選擇)。動態UIPickerView菜單
我遇到的問題是,當第二個字段被點擊時(第一個選擇被創建並顯示在第一個文本標記中),選擇器出現時,菜單不顯示第二個選擇階段。
import UIKit
class ViewController: UIViewController, UITextFieldDelegate, UIPickerViewDataSource, UIPickerViewDelegate {
@IBOutlet var textField1: UITextField!
@IBOutlet var textField2: UITextField!
var activeTextField = 0
@IBOutlet var picker: UIPickerView!
var characters = ["Letter", "Number"]
var letters = ["A", "B", "C", "D"]
var numbers = ["1", "2", "3", "4", "5"]
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
picker.hidden = true
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func textFieldDidBeginEditing(textField: UITextField) {
textField.resignFirstResponder()
if (textField == textField1) {
activeTextField = 0
}
else if (textField == textField2) {
activeTextField = 1
}
picker.hidden = false
}
func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
if (activeTextField == 0) {
return characters.count
}
else if (textField1.text == "Letter") {
return letters.count
}
else if (textField1.text == "Number") {
return numbers.count
}
return component
}
func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
picker.hidden = true
if (activeTextField == 0) {
textField1.text = characters[row]
picker.reloadAllComponents()
}
else if (textField1.text == "Letter") {
textField2.text = letters[row]
}
else if (textField1.text == "Number") {
textField2.text = numbers[row]
}
}
func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String! {
if (activeTextField == 0) {
return characters[row]
}
else if (textField1.text == "Letter") {
return letters[row]
}
else if (textField1.text == "Number") {
return numbers[row]
}
return nil
}
deinit {
self.picker.delegate = nil
self.picker.dataSource = nil
}
}
我也想指出的是,點擊了幾次,然後通過選擇器滾動並進行選擇後,菜單保持不變(仍顯示「信」和「號碼」),但實際選擇的值是「正確的」。請幫我做這個工作。提前致謝。
固定它。非常感謝。 – BadLuckJ 2015-02-23 10:56:33
不客氣:-) – 2015-02-24 15:17:40