I want to pass text of textField in custom class to ViewController and populate it to array when BarButtonItem of DatePicker is tapped. I used a callback as completion handler, but it caught EXC-BAD-ACCESS. What made this error and how could I pass text to my ViewController?
Custom class of textField
class HourDatePicker: UITextField {
var datePicker = UIDatePicker()
override init(frame: CGRect) {
super.init(frame: frame)
commominit()
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
commominit()
}
func commominit(){
text = ""
datePicker.datePickerMode = .dateAndTime
datePicker.minuteInterval = 30
datePicker.locale = Locale(identifier: "ja")
datePicker.addTarget(self, action: #selector(setText), for: .valueChanged)
setText()
inputView = datePicker
inputAccessoryView = customPicker()
}
#objc func setText(){
let f = DateFormatter()
f.dateStyle = .full
f.timeStyle = .short
f.locale = Locale(identifier: "ja")
textColor = .black
text = "\(f.string(from: datePicker.date))"
}
private func customPicker() -> UIToolbar {
let toolbar = UIToolbar()
toolbar.frame = CGRect(x: 0, y: 0, width: self.frame.size.width, height: 40)
let space = UIBarButtonItem(barButtonSystemItem: .fixedSpace, target: self, action: nil)
space.width = 100
let flexSpaceItem = UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: self, action: nil)
let todayButtonItem = UIBarButtonItem(title: "today", style: .done, target: self, action: #selector(setToday))
let selectButtonItem = UIBarButtonItem(title: "select", style: .done, target: self, action: #selector(tellCalenderText))
let toolbarItem = [space, flexSpaceItem, todayButtonItem, selectButtonItem]
toolbar.setItems(toolbarItem, animated: true)
}
return toolbar
}
#objc func tellCalenderText(completion: ((_ titleText: String) -> Void)){
//I want to pass text here.
if text != "" {
guard let titleText = text else {return}
completion(titleText)
} else {
return
}
}
ViewController
class Calender1ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
private let selectDate = HourDatePicker()
private var keepDate: [String] = []
#IBOutlet weak var timeTextView: UITextView!
#IBOutlet weak var dateText: HourDatePicker!
#IBOutlet weak var calenderTableView: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
self.calenderTableView.delegate = self
self.calenderTableView.dataSource = self
selectDate.tellCalenderText {[weak self] (titleText) in
self?.bringDate(title: titleText)
}
}
func bringDate(title: String){
print("title: \(title)")
self.keepDate.append(title)
timeTextView.text.append(contentsOf: "\(title)\n")
}
Thank you.
This is error log.
error log
First, I think you're getting a bit messed up by having:
private let selectDate = HourDatePicker()
and having:
#IBOutlet weak var dateText: HourDatePicker!
and then making use of selectDate inside viewDidLoad()...
Give this a try. I only made a few changes, and tried to include enough comments to make it clear. I think you'll find this a much more straight-forward way of getting your custom HourDatePicker class to pass information back to the view controller:
class HourDatePicker: UITextField {
var datePicker = UIDatePicker()
override init(frame: CGRect) {
super.init(frame: frame)
commominit()
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
commominit()
}
func commominit(){
text = ""
datePicker.datePickerMode = .dateAndTime
datePicker.minuteInterval = 30
datePicker.locale = Locale(identifier: "ja")
datePicker.addTarget(self, action: #selector(setText), for: .valueChanged)
setText()
inputView = datePicker
inputAccessoryView = customPicker()
}
#objc func setToday(){
datePicker.date = Date()
}
#objc func setText(){
let f = DateFormatter()
f.dateStyle = .full
f.timeStyle = .short
f.locale = Locale(identifier: "ja")
textColor = .black
text = "\(f.string(from: datePicker.date))"
}
private func customPicker() -> UIToolbar {
let toolbar = UIToolbar()
toolbar.frame = CGRect(x: 0, y: 0, width: self.frame.size.width, height: 40)
let space = UIBarButtonItem(barButtonSystemItem: .fixedSpace, target: self, action: nil)
space.width = 100
let flexSpaceItem = UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: self, action: nil)
let todayButtonItem = UIBarButtonItem(title: "today", style: .done, target: self, action: #selector(setToday))
let selectButtonItem = UIBarButtonItem(title: "select", style: .done, target: self, action: #selector(tellCalenderText))
let toolbarItem = [space, flexSpaceItem, todayButtonItem, selectButtonItem]
toolbar.setItems(toolbarItem, animated: true)
return toolbar
}
// "callback" closure
var tellController: ((String) ->())?
// triggered by "select" bar button tap
#objc func tellCalenderText() -> Void {
// get text from self
guard let t = text else {
return
}
// execute the callback closure
tellController?(t)
}
// #objc func tellCalenderText(completion: ((_ titleText: String) -> Void)){
// //I want to pass text here.
// if text != "" {
// guard let titleText = text else {return}
// completion(titleText)
// } else {
// return
// }
//
// }
}
//ViewController
class Calender1ViewController: UIViewController {
// not needed
//private let selectDate = HourDatePicker()
private var keepDate: [String] = []
// UITextView connected via Storyboard
#IBOutlet weak var timeTextView: UITextView!
// UITextField set to HourDatePicker, connected via Storyboard
#IBOutlet weak var dateText: HourDatePicker!
#IBOutlet weak var calenderTableView: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
// set the callback closure for the HourDatePicker
dateText.tellController = { [weak self] (titleText) in
self?.bringDate(title: titleText)
}
// not needed
//selectDate.tellCalenderText {[weak self] (titleText) in
// self?.bringDate(title: titleText)
//}
}
func bringDate(title: String){
print("title: \(title)")
self.keepDate.append(title)
timeTextView.text.append(contentsOf: "\(title)\n")
}
}
Related
I have a successful date picker and on the following page a stepper/number picker that allows users to select dates, and item quantities. In a new view, I would like to display all the previous information for them to review. I have a few methods in place but they don't seem to work. I am guessing it is because they aren't textFields, and some extra code is needed in order to ask the UIdatepicker and Stepper to pass the information. Take a look below please :)
Here is my date picker View Controller
import UIKit
class StoreDatesViewController: UIViewController {
#IBOutlet weak var nextButton: UIButton!
#IBOutlet weak var dropOff: UITextField!
#IBOutlet weak var pickUp: UITextField!
let datePicker = UIDatePicker()
override func viewDidLoad() {
super.viewDidLoad()
showDatePicker()
}
override func viewWillAppear(_ animated: Bool) {
nextButton.isEnabled = true
}
func showDatePicker(){
//Format Date
datePicker.datePickerMode = .date
datePicker.preferredDatePickerStyle = .inline
datePicker.backgroundColor = UIColor(named: "yellow-2")
//ToolBar
let toolbar = UIToolbar();
toolbar.sizeToFit()
let toolbar2 = UIToolbar();
toolbar2.sizeToFit()
let doneButton = UIBarButtonItem(title: "Done", style: .plain, target: self, action: #selector(donedatePicker));
let cancelButton = UIBarButtonItem(title: "Cancel", style: .plain, target: self, action: #selector(cancelDatePicker));
let doneButton2 = UIBarButtonItem(title: "Done", style: .plain, target: self, action: #selector(donedatePicker2));
let cancelButton2 = UIBarButtonItem(title: "Cancel", style: .plain, target: self, action: #selector(cancelDatePicker2));
toolbar.setItems([doneButton,cancelButton], animated: false)
toolbar.tintColor = .white
toolbar.backgroundColor = .red
toolbar2.setItems([doneButton2,cancelButton2], animated: false)
toolbar2.tintColor = .white
dropOff.inputAccessoryView = toolbar
dropOff.inputView = datePicker
pickUp.inputAccessoryView = toolbar2
pickUp.inputView = datePicker
}
#objc func donedatePicker(){
let formatter = DateFormatter()
formatter.dateFormat = "dd/MM/yyyy"
dropOff.text = formatter.string(from: datePicker.date)
self.view.endEditing(true)
}
#objc func donedatePicker2(){
let formatter = DateFormatter()
formatter.dateFormat = "dd/MM/yyyy"
pickUp.text = formatter.string(from: datePicker.date)
self.view.endEditing(true)
}
#objc func cancelDatePicker(){
self.view.endEditing(true)
}
#objc func cancelDatePicker2(){
self.view.endEditing(true)
}
#IBAction func didTapNext() {
let dropOff = dropOff.text
let pickUp = pickUp.text
let storeDetailsVC = self.storyboard?.instantiateViewController(withIdentifier: "storeDetailsVC") as! storeDetailsViewController
storeDetailsVC.dropOff = dropOff!
storeDetailsVC.pickUp = pickUp!
}
}
Here is my number Pick View Controller
import UIKit
class StoreQuantityViewController: UIViewController{
#IBOutlet weak var numbers: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
}
#IBAction func stepper(_ sender: UIStepper) {
numbers.text = String(Int(sender.value))
}
#IBAction func next(_ sender: Any) {
let data2 = numbers.text
let storeDetailsVC = self.storyboard?.instantiateViewController(withIdentifier: "storeDetailsVC") as! storeDetailsViewController
storeDetailsVC.data2 = data2!
self.navigationController?.pushViewController(storeDetailsVC, animated: true)
}
}
```
You need to pass the information from text fields to Strings in the next View.
Solution 1:
override func viewDidLoad() {
super.viewDidLoad()
pickUp.text = currentDate // This you can create using the date formatter
dropOff.text = defaultDate // This is default date value you can set if needed
showDatePicker()
}
Solution 2:
#IBAction func didTapNext() {
//Apply validation here to check if its empty if no default value provided.
if dropOff.text.isEmpty {
//Show alert to user to select drof off date.
return
}
if pickUp.text.isEmpty {
//Show alert to user to select PickUp date.
return
}
let dropOff = dropOff.text
let pickUp = pickUp.text
let storeDetailsVC = self.storyboard?.instantiateViewController(withIdentifier: "storeDetailsVC") as! storeDetailsViewController
storeDetailsVC.dropOff = dropOff!
storeDetailsVC.pickUp = pickUp!
}
The user input is typed into the TextFields and TextViews on ViewController1, but when I select a button to show either ViewController2 or ViewController3 via a segue, on my return to ViewController1 (also via another segue), the input is no longer there, as if the app was just reopened.
How do I make the initial user input remain in the text fields and text views while the user switches to a different view and also until the user hits the "Send Email" button back on ViewController1?
Below is my code
ViewController1
import UIKit
class ViewController: UIViewController {
#IBOutlet weak var DateTextField: UITextField!
#IBOutlet weak var ScrollView: UIScrollView!
#IBOutlet weak var FirstTextView: UITextField!
#IBOutlet weak var FirstName: UITextField!
#IBOutlet weak var OtherDetailsField: UITextView!
lazy var datePicker: UIDatePicker = {
let picker = UIDatePicker()
picker.datePickerMode = .date
picker.addTarget(self, action: #selector(datePickerChanged(_:)), for: .valueChanged)
return picker
}()
lazy var dateFormatter: DateFormatter = {
let formatter = DateFormatter()
formatter.dateStyle = .medium
formatter.timeStyle = .none
return formatter
}()
// Adjust Scroll for Keyboard ------------------
#objc func adjustForKeyboard(notification: Notification) {
guard let keyboardValue = notification.userInfo?[UIResponder.keyboardFrameEndUserInfoKey] as? NSValue else { return }
let keyboardScreenEndFrame = keyboardValue.cgRectValue
let keyboardViewEndFrame = view.convert(keyboardScreenEndFrame, from: view.window)
if notification.name == UIResponder.keyboardWillHideNotification {
ScrollView.contentInset = .zero
} else {
ScrollView.contentInset = UIEdgeInsets(top: 0, left: 0, bottom: keyboardViewEndFrame.height - view.safeAreaInsets.bottom, right: 0)
}
ScrollView.scrollIndicatorInsets = ScrollView.contentInset
// let selectedRange = OtherDetailsField.selectedRange
// OtherDetailsField.scrollRangeToVisible(selectedRange)
}
override func viewDidLoad(){
super.viewDidLoad()
// Adjust Scroll for Keyboard ---------------
let notificationCenter = NotificationCenter.default
notificationCenter.addObserver(self, selector: #selector(adjustForKeyboard), name: UIResponder.keyboardWillHideNotification, object: nil)
notificationCenter.addObserver(self, selector: #selector(adjustForKeyboard), name: UIResponder.keyboardWillChangeFrameNotification, object: nil)
// Date Picker ---------------
DateTextField.inputView = datePicker}
#objc func datePickerChanged(_ sender: UIDatePicker){
DateTextField.text = dateFormatter.string(from: sender.date)
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?){view.endEditing(true)
}
// Dismiss Keyboard ------------------
func setupKeyboardDismissRecognizer(){
let tapRecognizer: UITapGestureRecognizer = UITapGestureRecognizer(
target: self, action: #selector(ViewController.dismissKeyboard))
tapRecognizer.cancelsTouchesInView = false
self.view.addGestureRecognizer(tapRecognizer)
}
#objc func dismissKeyboard() {
view.endEditing(true)
}
}
// Add Done Button to keypad toolbar -----------------
extension UITextField{
#IBInspectable var doneAccessory: Bool{
get{
return self.doneAccessory
}
set (hasDone) {
if hasDone{
addDoneButtonOnKeyboard()
}
}
}
func addDoneButtonOnKeyboard() {
let doneToolbar: UIToolbar = UIToolbar(frame: CGRect.init(x: 0, y: 0, width: UIScreen.main.bounds.width, height: 50))
doneToolbar.barStyle = .default
let flexSpace = UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: nil, action: nil)
let done: UIBarButtonItem = UIBarButtonItem(title: "Done", style: .done, target: self, action: #selector(self.doneButtonAction))
let items = [flexSpace, done]
doneToolbar.items = items
doneToolbar.sizeToFit()
self.inputAccessoryView = doneToolbar
}
#objc func doneButtonAction()
{
self.resignFirstResponder()
}
}
extension UITextView{
#IBInspectable var doneAccessory: Bool{
get{
return self.doneAccessory
}
set (hasDone) {
if hasDone{
addDoneButtonOnKeyboard()
}
}
}
func addDoneButtonOnKeyboard() {
let doneToolbar: UIToolbar = UIToolbar(frame: CGRect.init(x: 0, y: 0, width: UIScreen.main.bounds.width, height: 50))
doneToolbar.barStyle = .default
let flexSpace = UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: nil, action: nil)
let done: UIBarButtonItem = UIBarButtonItem(title: "Done", style: .done, target: self, action: #selector(self.doneButtonAction))
let items = [flexSpace, done]
doneToolbar.items = items
doneToolbar.sizeToFit()
self.inputAccessoryView = doneToolbar
}
#objc func doneButtonAction() {
self.resignFirstResponder()
}
}
ViewController2
import UIKit
class ViewController2: UIViewController {
#IBOutlet weak var ScrollView: UIScrollView!
// Adjust Scroll for Keyboard ------------------
#objc func adjustForKeyboard(notification: Notification) {
guard let keyboardValue = notification.userInfo?[UIResponder.keyboardFrameEndUserInfoKey] as? NSValue else { return }
let keyboardScreenEndFrame = keyboardValue.cgRectValue
let keyboardViewEndFrame = view.convert(keyboardScreenEndFrame, from: view.window)
if notification.name == UIResponder.keyboardWillHideNotification {
ScrollView.contentInset = .zero
} else {
ScrollView.contentInset = UIEdgeInsets(top: 0, left: 0, bottom: keyboardViewEndFrame.height - view.safeAreaInsets.bottom, right: 0)
}
ScrollView.scrollIndicatorInsets = ScrollView.contentInset
//let selectedRange = yourTextView.selectedRange
//yourTextView.scrollRangeToVisible(selectedRange)
}
override func viewDidLoad() {
super.viewDidLoad()
// Adjust Scroll for Keyboard ---------------
let notificationCenter = NotificationCenter.default
notificationCenter.addObserver(self, selector: #selector(adjustForKeyboard), name: UIResponder.keyboardWillHideNotification, object: nil)
notificationCenter.addObserver(self, selector: #selector(adjustForKeyboard), name: UIResponder.keyboardWillChangeFrameNotification, object: nil)
}
// Dismiss Keyboard ------------------
func setupKeyboardDismissRecognizer(){
let tapRecognizer: UITapGestureRecognizer = UITapGestureRecognizer(
target: self, action: #selector(ViewController.dismissKeyboard))
tapRecognizer.cancelsTouchesInView = false
self.view.addGestureRecognizer(tapRecognizer)
}
func dismissKeyboard()
{
view.endEditing(true)
}
}
ViewController3
import UIKit
class ViewController3: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
}
}
short answer: backwards segues that You used create new instances, You could use 'dismiss' method instead or use Tab bar controller.
long answer:
Firstly, I assume You created Single View Controller when creating project in Xcode, and all segues You used are of "show" type.
Lets say You create segue 'ViewController1->ViewController2' and button 'NEXT' to execute it.
When You click on 'NEXT' You create a new instance of ViewController2, something kinda "new copy".
Next You create another segue to go back:
'ViewController2->ViewController1' and button 'BACK' to execute.
when You click on 'BACK' button (which triggers segue), You're NOT going back to the original ViewController1, but You're creating a new instance of ViewController1.
So as You can see, using segues this way can create future memory problems when users could go back and forth, every time creating new instance, stacking windows on top of each other.
The easiest solution is to delete segues that point backwards and instead put 'dismiss' method inside your 'BACK' button codeblock:
self.dismiss(animated: true, completion: nil)
Every time You use 'dismiss' method, You close the actual ViewController and go back to the previous one.
If You want something like 'HOME' button to go from any ViewController back to the first one (root), You can use this code:
self.view.window!.rootViewController?.dismiss(animated: true,completion: nil)
But remember - If You don't write some code to save Your data before dismissing, You will still lose the ViewController2 and ViewController3 data when You dismiss them.
Finally, if You need to keep Your data displayed when user switches between ViewControllers, I would use Tab bar controller. You can create it using template 'Tabbed App' when You create Xcode project.
Hope this helps! And don't take it as 100%, I'm still a Swift Rookie :)
Problem
I have an application that has a user registration view. It has many UITextField, and many of these have a picker with a toolbar embedded to close the picker i.e:
myTextField.inputView = myPicker
myTextField.inputAccessoryView = myToolbar
Essentially I want to reuse these text fields in different parts of my application, so I thought of subclassing UITextField, something like PickerUITextField.
Attempt
I've tried something like this:
class PickerUITextField: UITextField {
let picker = UIPickerView()
let toolbar = UIToolbar()
override init(frame: CGRect) {
super.init(frame: frame)
setup()
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
setup()
}
private func setup() {
withToolbar()
self.inputView = self.picker
self.inputAccessoryView = self.toolbar
}
private func withToolbar() {
toolbar.barStyle = UIBarStyle.default
toolbar.isTranslucent = true
let space = UIBarButtonItem(barButtonSystemItem: .flexibleSpace,
target: nil, action: nil)
let doneButton = UIBarButtonItem(title: "Done", style: .done,
target: self, action: #selector(removeToolBar))
toolbar.setItems([space, doneButton], animated: false)
toolbar.isUserInteractionEnabled = true
toolbar.sizeToFit()
}
#objc func removeToolBar() {
self.resignFirstResponder()
}
}
Question
However, how can I detect in the view controller that the user has pressed the "Done" button of my PickerUITextField? In other words:
class UserRegistrationViewController: UIViewController {
#IBOutlet weak var country: PickerUITextField!
// I want this to be triggered whenever the country picker closes
func didSelectCountry() {
print("User selected \(country.text!)")
}
}
Thank you for the help.
You can create a closure in PickerUITextField to perform done button action.
class PickerUITextField: UITextField {
let picker = UIPickerView()
let toolbar = UIToolbar()
var doneBtnAction:(() -> Void)?
override init(frame: CGRect) {
super.init(frame: frame)
setup()
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
setup()
}
private func setup() {
withToolbar()
self.inputView = self.picker
self.inputAccessoryView = self.toolbar
}
private func withToolbar() {
toolbar.barStyle = UIBarStyle.default
toolbar.isTranslucent = true
let space = UIBarButtonItem(barButtonSystemItem: .flexibleSpace,
target: nil, action: nil)
let doneButton = UIBarButtonItem(title: "Done", style: .done,
target: self, action: #selector(removeToolBar))
toolbar.setItems([space, doneButton], animated: false)
toolbar.isUserInteractionEnabled = true
toolbar.sizeToFit()
}
#objc func removeToolBar() {
doneBtnAction?()
self.resignFirstResponder()
}
}
And in your view controller, you can assign a closure. It will be called when you tap the done button.
class UserRegistrationViewController: UIViewController {
#IBOutlet weak var country: PickerUITextField!
override func viewDidLoad() {
super.viewDidLoad()
country.doneBtnAction = { [weak self] in
print("User selected \(self?.country.text!)")
}
}
}
You can use a protocol/delegate:
protocol PickerUITextFieldDelegate: class {
func didSelectCountry()
}
class PickerUITextField: UITextField {
// UITextField already have a 'delegate' we need a different name
weak var pickerDelegate: PickerUITextFieldDelegate?
let picker = UIPickerView()
let toolbar = UIToolbar()
#objc func removeToolBar() {
self.resignFirstResponder()
self.pickerDelegate?.didSelectCountry()
}
}
// We need to implement the PickerUITextFieldDelegate delegate here:
class UserRegistrationViewController: UIViewController, PickerUITextFieldDelegate {
#IBOutlet weak var country: PickerUITextField!
// Don't forget to set the delegate!
override func viewDidLoad() {
super.viewDidLoad()
self.country.pickerDelegate = self
}
// This will now be triggered by the delegate
func didSelectCountry() {
print("User selected \(country.text!)")
}
}
I have a class for constructing UIBarButtonItems:
enum KeyboardToolbarButton: Int {
case done = 0
case cancel
case back, backDisabled
case forward, forwardDisabled
func createButton(target: Any?, action: Selector?) -> UIBarButtonItem {
var button: UIBarButtonItem!
switch self {
case .back:
button = UIBarButtonItem(title: "<=", style: .plain, target: target, action: action)
case .backDisabled:
button = UIBarButtonItem(title: "<=", style: .plain, target: target, action: action)
button.isEnabled = false
case .forward:
button = UIBarButtonItem(title: "=>", style: .plain, target: target, action: action)
case .forwardDisabled:
button = UIBarButtonItem(title: "=>", style: .plain, target: target, action: action)
button.isEnabled = false
case .done:
button = UIBarButtonItem(title: "DONE", style: .plain, target: target, action: action)
case .cancel:
button = UIBarButtonItem(title: "CANCEL", style: .plain, target: target, action: action)
}
button.tag = rawValue
return button
}
static func detectType(barButton: UIBarButtonItem) -> KeyboardToolbarButton? {
return KeyboardToolbarButton(rawValue: barButton.tag)
}
}
Class for constructing KeyboardToolbar from KeyboardToolbarButton:
class KeyboardToolbar {
weak var toolBarDelegate: KeyboardToolbarDelegate?
var textField: UITextField!
init(textField: UITextField) {
self.textField = textField
self.textField.autocorrectionType = .no
self.textField.inputAssistantItem.leadingBarButtonGroups = []
self.textField.inputAssistantItem.trailingBarButtonGroups = []
}
func setup(leftButtons: [KeyboardToolbarButton], rightButtons: [KeyboardToolbarButton]) {
let leftBarButtons = leftButtons.map { (item) -> UIBarButtonItem in
return item.createButton(target: self, action: #selector(self.buttonTapped(sender:)))
}
let rightBarButtons = rightButtons.map { (item) -> UIBarButtonItem in
return item.createButton(target: self, action: #selector(self.buttonTapped(sender:)))
}
let groupLeading: UIBarButtonItemGroup = UIBarButtonItemGroup.init(barButtonItems: leftBarButtons, representativeItem: nil)
let groupTrailing: UIBarButtonItemGroup = UIBarButtonItemGroup.init(barButtonItems: rightBarButtons, representativeItem: nil)
textField.inputAssistantItem.leadingBarButtonGroups.append(groupLeading)
textField.inputAssistantItem.trailingBarButtonGroups.append(groupTrailing)
}
#objc func buttonTapped(sender: UIBarButtonItem) {
if let type = KeyboardToolbarButton.detectType(barButton: sender) {
print(type)
toolBarDelegate?.keyboardToolbar(button: sender, type: type, tappedIn: self)
}
}
}
And delegate:
protocol KeyboardToolbarDelegate: class {
func keyboardToolbar(button: UIBarButtonItem, type: KeyboardToolbarButton, tappedIn toolbar: KeyboardToolbar)
}
Here's how I use KeyboardToolbar:
class ViewController: UIViewController {
#IBOutlet weak var textField: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
addButtons(for: textField, setLeftButtons: [.back, .forward], andRightButtons: [.done])
}
private func addButtons(for textField: UITextField, setLeftButtons leftButtons: [KeyboardToolbarButton] = [], andRightButtons rightButtons: [KeyboardToolbarButton] = []) {
let toolbar = KeyboardToolbar(textField: textField)
toolbar.toolBarDelegate = self
toolbar.setup(leftButtons: leftButtons, rightButtons: rightButtons)
}
}
extension ViewController: KeyboardToolbarDelegate {
func keyboardToolbar(button: UIBarButtonItem, type: KeyboardToolbarButton, tappedIn toolbar: KeyboardToolbar) {
print("Tapped button type: \(type)")
}
}
Here's how it works (this feature available only on iPad)
So, the problem is that #objc func buttonTapped(sender: UIBarButtonItem) never calls. So, #selector(self.buttonTapped(sender:) does not connect to handler. How to fix it?
Update:
According to the answer of Taras Chernyshenko, I added KeyboardToolbar as a member of ViewController:
class ViewController: UIViewController {
#IBOutlet weak var textField: UITextField!
var toolbar: KeyboardToolbar!
override func viewDidLoad() {
super.viewDidLoad()
addButtons(for: textField, setLeftButtons: [.back, .forward], andRightButtons: [.done])
}
private func addButtons(for textField: UITextField, setLeftButtons leftButtons: [KeyboardToolbarButton] = [], andRightButtons rightButtons: [KeyboardToolbarButton] = []) {
toolbar = KeyboardToolbar(textField: textField)
toolbar.toolBarDelegate = self
toolbar.setup(leftButtons: leftButtons, rightButtons: rightButtons)
}
}
Problem is in you design. In KeyboardToolbar class in func setup(leftButtons:, rightButtons:) function you doing next:
let leftBarButtons = leftButtons.map { (item) -> UIBarButtonItem in
return item.createButton(target: self, action: #selector(self.buttonTapped(sender:)))
}
here you are setting action target to KeyboardToolbar class.
Next in addButtons(for textField:, setLeftButtons leftButtons:, andRightButtons rightButtons:) of ViewController you setup your buttons like
let toolbar = KeyboardToolbar(textField: textField)
toolbar.toolBarDelegate = self
toolbar.setup(leftButtons: leftButtons, rightButtons: rightButtons)
but after this function toolbar is dealocated, so actions can't reach their target.
To simple fix, store toolbar into class property
class ViewController: UIViewController {
#IBOutlet weak var textField: UITextField!
var toolbar = KeyboardToolbar?
override func viewDidLoad() {
super.viewDidLoad()
addButtons(for: textField, setLeftButtons: [.back, .forward], andRightButtons: [.done])
}
private func addButtons(for textField: UITextField, setLeftButtons leftButtons: [KeyboardToolbarButton] = [], andRightButtons rightButtons: [KeyboardToolbarButton] = []) {
let toolbar = KeyboardToolbar(textField: textField)
toolbar.toolBarDelegate = self
toolbar.setup(leftButtons: leftButtons, rightButtons: rightButtons)
self.toolbar = toolbar
}
}
In my ViewController there are several UiTextfield.
I intend to use a single UIDatePicker that is expected to respond accordingly.
my code only responds to event generated by second textfield and not the first one.
I just need to determine which UITextField has generated the event...
import UIKit
class ViewController: UIViewController , UITextFieldDelegate {
var datePicker = UIDatePicker()
#IBOutlet weak var tvDueDate: UITextField!
#IBOutlet weak var tvOtherDate: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
//setupDatePicker()
self.tvDueDate.tag = 0
self.tvOtherDate.tag = 1
//setupDatePicker(textField)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func textFieldDidEndEditing(textField: UITextField) {
setupDatePicker(textField)
}
func textFieldDidBeginEditing(textField: UITextField) {
setupDatePicker(textField)
}
func setupDatePicker(text: UITextField) {
// Sets up the "button"
//tvDueDate.text = "Pick a due date"
//tvDueDate.textAlignment = .Center
// Removes the indicator of the UITextField
//tvDueDate.tintColor = UIColor.clearColor()
// Specifies intput type
datePicker.datePickerMode = .Date
// Creates the toolbar
let toolBar = UIToolbar()
toolBar.barStyle = .Default
toolBar.translucent = true
toolBar.tintColor = UIColor(red: 92/255, green: 216/255, blue: 255/255, alpha: 1)
toolBar.sizeToFit()
// Adds the buttons
var doneButton = UIBarButtonItem(title: "Done", style: .Plain, target: self, action: "doneClick")
var spaceButton = UIBarButtonItem(barButtonSystemItem: .FlexibleSpace, target: nil, action: nil)
var cancelButton = UIBarButtonItem(title: "Cancel", style: .Plain, target: self, action: "cancelClick")
toolBar.setItems([cancelButton, spaceButton, doneButton], animated: false)
toolBar.userInteractionEnabled = true
// Adds the toolbar to the view
if text.tag == 0{
self.tvDueDate.inputView = datePicker
self.tvDueDate.inputAccessoryView = toolBar
}
else
{
self.tvOtherDate.inputView = datePicker
self.tvOtherDate.inputAccessoryView = toolBar
}
}
func doneClick() {
var dateFormatter = NSDateFormatter()
//dateFormatter.dateFormat = "dd-MM-yyyy"
dateFormatter.dateStyle = .ShortStyle
if tvDueDate.isFirstResponder(){
tvDueDate.text = dateFormatter.stringFromDate(datePicker.date)
tvDueDate.resignFirstResponder()
}else{
tvOtherDate.text = dateFormatter.stringFromDate(datePicker.date)
tvOtherDate.resignFirstResponder()
}
}
func cancelClick() {
if tvDueDate.isFirstResponder(){
tvDueDate.resignFirstResponder()
}else{
tvOtherDate.resignFirstResponder()
}
}
}
Give unique tags to your UITextFields and use UITextField delegates.
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
//setupDatePicker()
self.tvDueDate.tag = 0
self.tvOtherDate.tag = 1
self.tvDueDate.delegate = self
self.tvOtherDate.delegate = self
//setupDatePicker(textField)
}
// UITextField Delegates
func textFieldDidBeginEditing(textField: UITextField) {
println("TextField did begin editing method called")
if textField.tag == 0
..........
if textField.tag == 2
....
}
func textFieldDidEndEditing(textField: UITextField) {
println("TextField did end editing method called")
}
func textFieldShouldBeginEditing(textField: UITextField) -> Bool {
println("TextField should begin editing method called")
return true;
}
You are calling
setupDatePicker()
From viewDidLoad and your code is
if tvDueDate.isFirstResponder(){
tvDueDate.inputView = datePicker
tvDueDate.inputAccessoryView = toolBar
}
else
{
tvOtherDate.inputView = datePicker
tvOtherDate.inputAccessoryView = toolBar
}
adds picker as inputAccessoryView . As at the point of viewDidLoad no text field is FirstResponder that's why only your else part is excited and its only added to tvOtherDate field.
I would recommend to call it from the textfield delegate method (will begin editing or didbegineditting) or call it on tap. And to check from which it is called also pass the textfield or you can use tag.
Adding Delegates and tags to my UiTextField helped me.......
import UIKit
class ViewController: UIViewController , UITextFieldDelegate {
var datePicker = UIDatePicker()
#IBOutlet weak var tvDueDate: UITextField!
#IBOutlet weak var tvOtherDate: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
//setupDatePicker()
self.tvDueDate.tag = 0
self.tvOtherDate.tag = 1
//setupDatePicker(textField)
self.tvDueDate.delegate = self
self.tvOtherDate.delegate = self
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func textFieldDidEndEditing(textField: UITextField) {
setupDatePicker(textField)
}
func textFieldDidBeginEditing(textField: UITextField) {
setupDatePicker(textField)
}
func setupDatePicker(text: UITextField) {
// Sets up the "button"
//tvDueDate.text = "Pick a due date"
//tvDueDate.textAlignment = .Center
// Removes the indicator of the UITextField
//tvDueDate.tintColor = UIColor.clearColor()
// Specifies intput type
datePicker.datePickerMode = .Date
// Creates the toolbar
let toolBar = UIToolbar()
toolBar.barStyle = .Default
toolBar.translucent = true
toolBar.tintColor = UIColor(red: 92/255, green: 216/255, blue: 255/255, alpha: 1)
toolBar.sizeToFit()
// Adds the buttons
var doneButton = UIBarButtonItem(title: "Done", style: .Plain, target: self, action: "doneClick")
var spaceButton = UIBarButtonItem(barButtonSystemItem: .FlexibleSpace, target: nil, action: nil)
var cancelButton = UIBarButtonItem(title: "Cancel", style: .Plain, target: self, action: "cancelClick")
toolBar.setItems([cancelButton, spaceButton, doneButton], animated: false)
toolBar.userInteractionEnabled = true
// Adds the toolbar to the view
if text.tag == 0{
self.tvDueDate.inputView = datePicker
self.tvDueDate.inputAccessoryView = toolBar
}
else
{
self.tvOtherDate.inputView = datePicker
self.tvOtherDate.inputAccessoryView = toolBar
}
}
func doneClick() {
var dateFormatter = NSDateFormatter()
//dateFormatter.dateFormat = "dd-MM-yyyy"
dateFormatter.dateStyle = .ShortStyle
if tvDueDate.isFirstResponder(){
tvDueDate.text = dateFormatter.stringFromDate(datePicker.date)
tvDueDate.resignFirstResponder()
}else{
tvOtherDate.text = dateFormatter.stringFromDate(datePicker.date)
tvOtherDate.resignFirstResponder()
}
}
func cancelClick() {
if tvDueDate.isFirstResponder(){
tvDueDate.resignFirstResponder()
}else{
tvOtherDate.resignFirstResponder()
}
}
}