我是新来的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