Using a UIDatePicker for multiple UITextFields [duplicate]












-1
















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.










share|improve this question















marked as duplicate by Sh_Khan ios
Users with the  ios badge can single-handedly close ios questions as duplicates and reopen them as needed.

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 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


















-1
















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.










share|improve this question















marked as duplicate by Sh_Khan ios
Users with the  ios badge can single-handedly close ios questions as duplicates and reopen them as needed.

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 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
















-1












-1








-1









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.










share|improve this question

















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 28 '18 at 12:31







chumps52

















asked Nov 28 '18 at 12:23









chumps52chumps52

487




487




marked as duplicate by Sh_Khan ios
Users with the  ios badge can single-handedly close ios questions as duplicates and reopen them as needed.

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 ios
Users with the  ios badge can single-handedly close ios questions as duplicates and reopen them as needed.

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 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





















  • "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



















"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














1 Answer
1






active

oldest

votes


















2














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.






share|improve this answer


























  • 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


















1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









2














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.






share|improve this answer


























  • 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
















2














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.






share|improve this answer


























  • 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














2












2








2







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.






share|improve this answer















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.







share|improve this answer














share|improve this answer



share|improve this answer








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



















  • 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





Popular posts from this blog

A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks

Calculate evaluation metrics using cross_val_predict sklearn

Insert data from modal to MySQL (multiple modal on website)