是否有可能从Swift中的UITextField中获得UIPickerView和UIDatePicker?
我是新来的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
}
}
你可以将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)
}
}
}
感谢user4130613,我添加了一些更多的信息,因为我得到了一些错误...谢谢 – Nick89 2015-03-30 22:48:13
对不起,我仍然有麻烦,并真的想知道我要去哪里错了。我在上面的问题中添加了我的新代码,以便您可以看到我的所有代码。我收到一个错误,说'使用未解析的标识符'pickerViewInstance'和'datePickerInstance'。我真的很感谢你帮助我们理解我做错了什么,因为我对这一切都很陌生,并且热衷于让我的头脑得到解决!在此先感谢 – Nick89 2015-04-01 22:03:42
@ Nick89这是因为您尚未在ViewController类中定义pickerViewInstance和datePickerInstance,因此请定义它们并在ViewController中初始化它们 – Ch0k0l8 2015-04-02 08:28:09
做一个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