我是新來的Swift,請耐心等待。是否有可能從Swift中的UITextField中獲得UIPickerView和UIDatePicker?
首先,讓我解釋一下我的應用程序佈局:
我有一個文本字段在屏幕上,有一個標籤和一個按鈕。我詢問用戶3個問題 - 使用「標籤」顯示這些問題,並且每當用戶按下下一個「按鈕」時(問題從數組中填充),就會出現下一個問題。
其中一個問題需要填充Picker View(因此用戶可以選擇一個選項)。 其中一個問題使用普通鍵盤。 而我需要使用日期選取器的最後一個問題。
這可能嗎?如果是這樣,你能告訴我如何將它添加到我的代碼下面嗎?
let questions = ["What is your title?", "What is your full name?", "When were you born?"]
var currentQuestionIndex = 0
let placeholder = ["Title", "Full name", "Date of birth"]
var currentPlaceholderIndex = 0
@IBAction func nextButton(sender: AnyObject) {
textField.hidden = false
textField.placeholder = placeholder[currentPlaceholderIndex]
barImage.hidden = false
// Reset text field to have no text
textField.text = ""
questionLabel.text = questions[currentQuestionIndex]
// Displays the questions in array
if currentQuestionIndex < questions.count {
++currentQuestionIndex
// Displays the placeholder text in the textfield
if currentPlaceholderIndex < placeholder.count {
++currentPlaceholderIndex
}
buttonLabel.setTitle("Next", forState: UIControlState.Normal)
}
else {
//Add some logic here to run whenever the user has answered all the questions.
}
}
感謝您的幫助!
尼克
NEW下面的代碼(從2015年4月6日):
類的ViewController:UIViewController中,UITextFieldDelegate,UIPickerViewDataSource,UIPickerViewDelegate基於currentQuestionIndex
{
var pickerData = ["Mr", "Mrs", "Miss", "Ms", "Doctor"]
var questions = ["What is your title?", "What is your name?", "What is your DOB?"]
var currentQuestionIndex = 0
var datePickerView:UIDatePicker = UIDatePicker()
var pickerView:UIPickerView = UIPickerView()
@IBOutlet var buttonLabel: UIButton!
@IBOutlet var questionsLabel: UILabel!
@IBOutlet var textField: UITextField!
@IBAction func textFieldEditing(sender: UITextField) {
datePickerView.datePickerMode = UIDatePickerMode.Date
sender.inputView = datePickerView
datePickerView.addTarget(self, action: Selector("datePickerValueChanged:"), forControlEvents: UIControlEvents.ValueChanged)
}
func datePickerValueChanged(sender: UIDatePicker) {
var dateformatter = NSDateFormatter()
dateformatter.dateStyle = NSDateFormatterStyle.LongStyle
textField.text = dateformatter.stringFromDate(sender.date)
}
@IBAction func nextButton(sender: UIButton) {
textField.text = ""
textField.hidden = false
buttonLabel.setTitle("Next", forState: UIControlState.Normal)
questionsLabel.text = questions[currentQuestionIndex]
if currentQuestionIndex < questions.count {
currentQuestionIndex++
}
else {
//println("questions are complete")
}
}
func textFieldDidBeginEditing(textField: UITextField) {
switch currentQuestionIndex {
case 0:
self.view.addSubview(datePickerView)
case 2:
self.view.addSubview(pickerView)
default:
println("Test")
}
}
override func viewDidLoad() {
super.viewDidLoad()
buttonLabel.setTitle("Start", forState: UIControlState.Normal)
textField.hidden = true
questionsLabel.text = ""
textField.delegate = self
pickerView.delegate = self
pickerView.dataSource = self
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func textFieldShouldReturn(textField: UITextField) -> Bool {
textField.resignFirstResponder()
return true
}
override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
self.view.endEditing(true)
}
func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return pickerData.count
}
func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String! {
return pickerData[row]
}
func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
textField.text = pickerData[row]
pickerView.hidden = true
}
}
做一個IBOulet爲您要使用的每一個元素,檢查你需要所述元件,顯示這個元素,其questione並隱藏其他 – milo526 2015-03-30 21:42:05
@ Nick89只是回到看看您是否可以使用該解決方案,但我發現代碼中存在問題。你放入nextButton(:)的函數textFieldDidBeginEditing(:)應該不在這個函數中。 textFieldDidBeginEditing(:)是您遵循的UITextFieldDelegate中聲明的方法,現在您應該實現它。請記住,在協議中聲明(未定義)的方法只是抽象的,真正的實現在你的ViewController中出現。 – Ch0k0l8 2015-04-04 16:26:55
@ Nick89讓我知道如果你有任何問題,並不要猶豫,問 – Ch0k0l8 2015-04-04 16:30:42