我是Swift的新手所以请耐心等待。
首先,让我解释一下我的应用布局:
我在屏幕上有一个文本字段,带有标签和按钮。我问用户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.
}
}
感谢您的帮助!
尼克
以下新代码(自2015年4月6日起):
类ViewController:UIViewController,UITextFieldDelegate,UIPickerViewDataSource,UIPickerViewDelegate {
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
}
}
答案 0 :(得分:0)
根据currentQuestionIndex
,您可以向UIPickerView
个实例添加UIDatePicker
或UITextField
switch currentQuestionIndex {
case 0:
// TODO: Add UIPickerView
case 2:
// TODO: Add UIDatePicker
}
编辑:更多解释
首先,遵守UITextFieldDelegate
并实施textFieldDidBeginEditing(:)
您必须实现该方法,因为您希望在UITextField实例获得第一响应者状态时获得通知
另外,将UITextField
实例的delegate
属性设置为self
,或者将要放置的实例/对象设置为textFieldDidBeginEditing(:)
然后编写此代码
func textFieldDidBeginEditing(_ textField: UITextField) {
switch currentQuestionIndex {
case 0:
// TODO: Add UIPickerView
// Add it for example at the bottom your view self.view.addSubview(pickerViewInstance)
// or add it as the subview of UIPopoverController instance that is added to your UITextField instance
case 2:
// TODO: Add UIDatePicker
// Add it for example at the bottom your view self.view.addSubview(datePickerInstance)
// or add it as the subview of UIPopoverController instance that is added to your UITextField instance
}
}
当您想要向UIPopoverViewController添加另一个视图时,请记住删除添加的视图。
编辑:添加真实世界的实现
class NewCaseViewController : UIViewController {
. . .
@IBOutlet weak var textField: UITextField // TODO: Initialize it properly in designated initializer
var popoverController: UIPopoverController! // TODO: Initialize it
. . .
}
class PopoverContentManager : UIViewController {
. . .
var pickerView: UIPickerView // TODO: Initialize it properly
var datePicker: UIDatePicker // TODO: Initialize it properly
. . .
}
extension NewCaseViewController : UITextFieldDelegate {
func textFieldBeginEditing(_ textField: UITextField) {
println(__FUNCTION__)
var popover = PopoverContentManager()
switch self.currentQuestionIndex {
case 0:
popover.datePicker.hidden = true
self.popoverController = init(contentViewController: popOver)
self.popoverController.presentPopoverFromRect(self.textField.bounds, inView: self.textField, permittedArrowDirections: UIPopoverArrowDirectionDown, animated: true)
self.popoverController.popoverContentSize = CGSizeMake(120, 90)
case 1:
popover.pickerView.hidden = true
self.popoverController = init(contentViewController: popOver)
self.popoverController.presentPopoverFromRect(self.textField.bounds, inView: self.textField, permittedArrowDirections: UIPopoverArrowDirectionDown, animated: true)
self.popoverController.popoverContentSize = CGSizeMake(120, 90)
}
}
}