Using a UIDatePicker for multiple UITextFields [duplicate]
This question already has an answer here:
Swift: One UITimePicker, Multiple Textfields
1 answer
I have two UITextFields
(but will eventually add more), created as outlets like so
@IBOutlet weak var startField: UITextField!
@IBOutlet weak var finishField: UITextField!
I'd like the input view for both to be a UIDatePicker
with datePickerMode = .time
and .dateFormat = "HH:mm"
.
How can I achieve this?
I've figured out how to get the format correct, but I'm not sure how to get both UITextFields
to work independently, everything I've tried so far affects just the first field (as below, as I'm unsure how to use tags in this situation).
(eg even when selecting finishField, startField is the one that is edited/populated).
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
let datePickerView: UIDatePicker = UIDatePicker()
datePickerView.datePickerMode = .time
startField.inputView = datePickerView
finishField.inputView = datePickerView
datePickerView.addTarget(self, action: #selector(ViewController.datePickerValueChanged), for: UIControl.Event.valueChanged)
}
@objc func datePickerValueChanged(sender: UIDatePicker) {
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "HH:mm"
startField.text = dateFormatter.string(from: sender.date)
}
I have tried using tags but it still seems to only affect startField.
ios swift uitextfield uidatepicker
marked as duplicate by Sh_Khan
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 28 '18 at 12:43
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
|
show 2 more comments
This question already has an answer here:
Swift: One UITimePicker, Multiple Textfields
1 answer
I have two UITextFields
(but will eventually add more), created as outlets like so
@IBOutlet weak var startField: UITextField!
@IBOutlet weak var finishField: UITextField!
I'd like the input view for both to be a UIDatePicker
with datePickerMode = .time
and .dateFormat = "HH:mm"
.
How can I achieve this?
I've figured out how to get the format correct, but I'm not sure how to get both UITextFields
to work independently, everything I've tried so far affects just the first field (as below, as I'm unsure how to use tags in this situation).
(eg even when selecting finishField, startField is the one that is edited/populated).
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
let datePickerView: UIDatePicker = UIDatePicker()
datePickerView.datePickerMode = .time
startField.inputView = datePickerView
finishField.inputView = datePickerView
datePickerView.addTarget(self, action: #selector(ViewController.datePickerValueChanged), for: UIControl.Event.valueChanged)
}
@objc func datePickerValueChanged(sender: UIDatePicker) {
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "HH:mm"
startField.text = dateFormatter.string(from: sender.date)
}
I have tried using tags but it still seems to only affect startField.
ios swift uitextfield uidatepicker
marked as duplicate by Sh_Khan
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 28 '18 at 12:43
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
"everything I've tried so far..." Please share it here by edit-ing the question.
– Rakesha Shastri
Nov 28 '18 at 12:27
Apologies, edited @RakeshaShastri
– chumps52
Nov 28 '18 at 12:32
nothing to apologize for here :D
– Rakesha Shastri
Nov 28 '18 at 12:36
@chumps52, this line causing the issuestartField.text = dateFormatter.string(from: sender.date)
Follow this Link : stackoverflow.com/questions/47358415/…
– Kuldeep
Nov 28 '18 at 12:39
1
@RakeshaShastri look carefully to the answer in duplicate it includestextfield.isFirstResponder
– Sh_Khan
Nov 28 '18 at 12:47
|
show 2 more comments
This question already has an answer here:
Swift: One UITimePicker, Multiple Textfields
1 answer
I have two UITextFields
(but will eventually add more), created as outlets like so
@IBOutlet weak var startField: UITextField!
@IBOutlet weak var finishField: UITextField!
I'd like the input view for both to be a UIDatePicker
with datePickerMode = .time
and .dateFormat = "HH:mm"
.
How can I achieve this?
I've figured out how to get the format correct, but I'm not sure how to get both UITextFields
to work independently, everything I've tried so far affects just the first field (as below, as I'm unsure how to use tags in this situation).
(eg even when selecting finishField, startField is the one that is edited/populated).
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
let datePickerView: UIDatePicker = UIDatePicker()
datePickerView.datePickerMode = .time
startField.inputView = datePickerView
finishField.inputView = datePickerView
datePickerView.addTarget(self, action: #selector(ViewController.datePickerValueChanged), for: UIControl.Event.valueChanged)
}
@objc func datePickerValueChanged(sender: UIDatePicker) {
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "HH:mm"
startField.text = dateFormatter.string(from: sender.date)
}
I have tried using tags but it still seems to only affect startField.
ios swift uitextfield uidatepicker
This question already has an answer here:
Swift: One UITimePicker, Multiple Textfields
1 answer
I have two UITextFields
(but will eventually add more), created as outlets like so
@IBOutlet weak var startField: UITextField!
@IBOutlet weak var finishField: UITextField!
I'd like the input view for both to be a UIDatePicker
with datePickerMode = .time
and .dateFormat = "HH:mm"
.
How can I achieve this?
I've figured out how to get the format correct, but I'm not sure how to get both UITextFields
to work independently, everything I've tried so far affects just the first field (as below, as I'm unsure how to use tags in this situation).
(eg even when selecting finishField, startField is the one that is edited/populated).
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
let datePickerView: UIDatePicker = UIDatePicker()
datePickerView.datePickerMode = .time
startField.inputView = datePickerView
finishField.inputView = datePickerView
datePickerView.addTarget(self, action: #selector(ViewController.datePickerValueChanged), for: UIControl.Event.valueChanged)
}
@objc func datePickerValueChanged(sender: UIDatePicker) {
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "HH:mm"
startField.text = dateFormatter.string(from: sender.date)
}
I have tried using tags but it still seems to only affect startField.
This question already has an answer here:
Swift: One UITimePicker, Multiple Textfields
1 answer
ios swift uitextfield uidatepicker
ios swift uitextfield uidatepicker
edited Nov 28 '18 at 12:31
chumps52
asked Nov 28 '18 at 12:23
chumps52chumps52
487
487
marked as duplicate by Sh_Khan
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 28 '18 at 12:43
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
marked as duplicate by Sh_Khan
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 28 '18 at 12:43
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
"everything I've tried so far..." Please share it here by edit-ing the question.
– Rakesha Shastri
Nov 28 '18 at 12:27
Apologies, edited @RakeshaShastri
– chumps52
Nov 28 '18 at 12:32
nothing to apologize for here :D
– Rakesha Shastri
Nov 28 '18 at 12:36
@chumps52, this line causing the issuestartField.text = dateFormatter.string(from: sender.date)
Follow this Link : stackoverflow.com/questions/47358415/…
– Kuldeep
Nov 28 '18 at 12:39
1
@RakeshaShastri look carefully to the answer in duplicate it includestextfield.isFirstResponder
– Sh_Khan
Nov 28 '18 at 12:47
|
show 2 more comments
"everything I've tried so far..." Please share it here by edit-ing the question.
– Rakesha Shastri
Nov 28 '18 at 12:27
Apologies, edited @RakeshaShastri
– chumps52
Nov 28 '18 at 12:32
nothing to apologize for here :D
– Rakesha Shastri
Nov 28 '18 at 12:36
@chumps52, this line causing the issuestartField.text = dateFormatter.string(from: sender.date)
Follow this Link : stackoverflow.com/questions/47358415/…
– Kuldeep
Nov 28 '18 at 12:39
1
@RakeshaShastri look carefully to the answer in duplicate it includestextfield.isFirstResponder
– Sh_Khan
Nov 28 '18 at 12:47
"everything I've tried so far..." Please share it here by edit-ing the question.
– Rakesha Shastri
Nov 28 '18 at 12:27
"everything I've tried so far..." Please share it here by edit-ing the question.
– Rakesha Shastri
Nov 28 '18 at 12:27
Apologies, edited @RakeshaShastri
– chumps52
Nov 28 '18 at 12:32
Apologies, edited @RakeshaShastri
– chumps52
Nov 28 '18 at 12:32
nothing to apologize for here :D
– Rakesha Shastri
Nov 28 '18 at 12:36
nothing to apologize for here :D
– Rakesha Shastri
Nov 28 '18 at 12:36
@chumps52, this line causing the issue
startField.text = dateFormatter.string(from: sender.date)
Follow this Link : stackoverflow.com/questions/47358415/…– Kuldeep
Nov 28 '18 at 12:39
@chumps52, this line causing the issue
startField.text = dateFormatter.string(from: sender.date)
Follow this Link : stackoverflow.com/questions/47358415/…– Kuldeep
Nov 28 '18 at 12:39
1
1
@RakeshaShastri look carefully to the answer in duplicate it includes
textfield.isFirstResponder
– Sh_Khan
Nov 28 '18 at 12:47
@RakeshaShastri look carefully to the answer in duplicate it includes
textfield.isFirstResponder
– Sh_Khan
Nov 28 '18 at 12:47
|
show 2 more comments
1 Answer
1
active
oldest
votes
You can check which text field is active in the selector and change the text accordingly.
@objc func datePickerValueChanged(sender: UIDatePicker) {
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "HH:mm"
if startTextField.isFirstResponder {
startField.text = dateFormatter.string(from: sender.date)
} else if finishTextField.isFirstResponder {
finishTextField.text = dateFormatter.string(from: sender.date)
}
// Handle the other cases if any
}
If there are a lot of text fields you could store them in an array and loop them and check which is active.
If i have 50 textfield then this approach is not good.
– Pankil
Nov 28 '18 at 12:44
That works, thank you! Tried doing similar with tags before and it didn't work, not sure why. I've deleted that code now anyway so can't check.
– chumps52
Nov 28 '18 at 12:45
@Pankil i just edited the answer as you commented :)
– Rakesha Shastri
Nov 28 '18 at 12:45
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
You can check which text field is active in the selector and change the text accordingly.
@objc func datePickerValueChanged(sender: UIDatePicker) {
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "HH:mm"
if startTextField.isFirstResponder {
startField.text = dateFormatter.string(from: sender.date)
} else if finishTextField.isFirstResponder {
finishTextField.text = dateFormatter.string(from: sender.date)
}
// Handle the other cases if any
}
If there are a lot of text fields you could store them in an array and loop them and check which is active.
If i have 50 textfield then this approach is not good.
– Pankil
Nov 28 '18 at 12:44
That works, thank you! Tried doing similar with tags before and it didn't work, not sure why. I've deleted that code now anyway so can't check.
– chumps52
Nov 28 '18 at 12:45
@Pankil i just edited the answer as you commented :)
– Rakesha Shastri
Nov 28 '18 at 12:45
add a comment |
You can check which text field is active in the selector and change the text accordingly.
@objc func datePickerValueChanged(sender: UIDatePicker) {
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "HH:mm"
if startTextField.isFirstResponder {
startField.text = dateFormatter.string(from: sender.date)
} else if finishTextField.isFirstResponder {
finishTextField.text = dateFormatter.string(from: sender.date)
}
// Handle the other cases if any
}
If there are a lot of text fields you could store them in an array and loop them and check which is active.
If i have 50 textfield then this approach is not good.
– Pankil
Nov 28 '18 at 12:44
That works, thank you! Tried doing similar with tags before and it didn't work, not sure why. I've deleted that code now anyway so can't check.
– chumps52
Nov 28 '18 at 12:45
@Pankil i just edited the answer as you commented :)
– Rakesha Shastri
Nov 28 '18 at 12:45
add a comment |
You can check which text field is active in the selector and change the text accordingly.
@objc func datePickerValueChanged(sender: UIDatePicker) {
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "HH:mm"
if startTextField.isFirstResponder {
startField.text = dateFormatter.string(from: sender.date)
} else if finishTextField.isFirstResponder {
finishTextField.text = dateFormatter.string(from: sender.date)
}
// Handle the other cases if any
}
If there are a lot of text fields you could store them in an array and loop them and check which is active.
You can check which text field is active in the selector and change the text accordingly.
@objc func datePickerValueChanged(sender: UIDatePicker) {
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "HH:mm"
if startTextField.isFirstResponder {
startField.text = dateFormatter.string(from: sender.date)
} else if finishTextField.isFirstResponder {
finishTextField.text = dateFormatter.string(from: sender.date)
}
// Handle the other cases if any
}
If there are a lot of text fields you could store them in an array and loop them and check which is active.
edited Nov 28 '18 at 12:44
answered Nov 28 '18 at 12:42
Rakesha ShastriRakesha Shastri
7,48121635
7,48121635
If i have 50 textfield then this approach is not good.
– Pankil
Nov 28 '18 at 12:44
That works, thank you! Tried doing similar with tags before and it didn't work, not sure why. I've deleted that code now anyway so can't check.
– chumps52
Nov 28 '18 at 12:45
@Pankil i just edited the answer as you commented :)
– Rakesha Shastri
Nov 28 '18 at 12:45
add a comment |
If i have 50 textfield then this approach is not good.
– Pankil
Nov 28 '18 at 12:44
That works, thank you! Tried doing similar with tags before and it didn't work, not sure why. I've deleted that code now anyway so can't check.
– chumps52
Nov 28 '18 at 12:45
@Pankil i just edited the answer as you commented :)
– Rakesha Shastri
Nov 28 '18 at 12:45
If i have 50 textfield then this approach is not good.
– Pankil
Nov 28 '18 at 12:44
If i have 50 textfield then this approach is not good.
– Pankil
Nov 28 '18 at 12:44
That works, thank you! Tried doing similar with tags before and it didn't work, not sure why. I've deleted that code now anyway so can't check.
– chumps52
Nov 28 '18 at 12:45
That works, thank you! Tried doing similar with tags before and it didn't work, not sure why. I've deleted that code now anyway so can't check.
– chumps52
Nov 28 '18 at 12:45
@Pankil i just edited the answer as you commented :)
– Rakesha Shastri
Nov 28 '18 at 12:45
@Pankil i just edited the answer as you commented :)
– Rakesha Shastri
Nov 28 '18 at 12:45
add a comment |
"everything I've tried so far..." Please share it here by edit-ing the question.
– Rakesha Shastri
Nov 28 '18 at 12:27
Apologies, edited @RakeshaShastri
– chumps52
Nov 28 '18 at 12:32
nothing to apologize for here :D
– Rakesha Shastri
Nov 28 '18 at 12:36
@chumps52, this line causing the issue
startField.text = dateFormatter.string(from: sender.date)
Follow this Link : stackoverflow.com/questions/47358415/…– Kuldeep
Nov 28 '18 at 12:39
1
@RakeshaShastri look carefully to the answer in duplicate it includes
textfield.isFirstResponder
– Sh_Khan
Nov 28 '18 at 12:47