I am using the CosmicMind library for Material design and I am trying to set up basic textfields with some error checking but the documentation is not very good.
I have set up my text fields like the following:
#IBOutlet weak var userNameField: ErrorTextField!
userNameField.placeholder = "Enter Username"
userNameField.delegate = self
userNameField.error = "Text is too long" // App Crashes here
userNameField.errorColor = Color.red.base
App crashes with EXC_BAD_ACCESS
I validate my field like this:
func textField(textField: TextField, didChange text: String?) {
if textField == userNameField {
if validateUsername(text: textField.text!) {
userNameField.isErrorRevealed = true
} else {
userNameField.isErrorRevealed = false
}
}
}
Even if I remove that line, app crashes on userNameField.isErrorRevealed = true too.
I have created following code with same library which are you using, which are working fine. I have create textField programatically.
import UIKit
import Material
class ViewController: UIViewController {
fileprivate var emailField: ErrorTextField!
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = Color.grey.lighten5
emailField = ErrorTextField()
emailField.placeholder = "Email"
emailField.error = "Text is too long"
emailField.delegate = self
self.view.layout(emailField).height(40).width(200).centerVertically().centerHorizontally()
}
}
extension ViewController: TextFieldDelegate {
public func textFieldDidEndEditing(_ textField: UITextField) {
(textField as? ErrorTextField)?.isErrorRevealed = false
}
public func textFieldShouldClear(_ textField: UITextField) -> Bool {
(textField as? ErrorTextField)?.isErrorRevealed = false
return true
}
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
(textField as? ErrorTextField)?.isErrorRevealed = true
return true
}
}
See this sample project: Sample Project
I hope this will help you.
Related
EDIT: New ErrorCurrently using XCode 12, and I'm trying to add a placeholder. I followed the Swift QuestionBot documentation but it doesn't work (I'm assuming it's because my XCode is much newer). Anyway, appreciate all the help!
EDIT: I added an image of a new error I got.
EDIT 2: Added MyQuestionAnswerer() struct! It's on a different view controller (obvious).
import UIKit
class ViewController: UIViewController {
#IBOutlet weak var responseLabel: UILabel!
#IBOutlet weak var askButton: UIButton!
#IBOutlet weak var questionField: UITextField!
let questionAnswerer = MyQuestionAnswerer()
override func viewDidLoad() {
super.viewDidLoad()
questionField.becomeFirstResponder()
}
func respondToQuestion(_ question: String) {
let answer = questionAnswerer.responseTo(question: question)
displayAnswerTextOnScreen(answer)
questionField.placeholder = "Ask another question..."
questionField.text = nil
askButton.isEnabled = false
}
#IBAction func askButtonTapped(_ sender: AnyObject) {
guard questionField.text != nil else {
return
}
questionField.resignFirstResponder()
}
func displayAnswerTextOnScreen(_ answer: String) {
responseLabel.text = answer
}
}
extension ViewController: UITextFieldDelegate {
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
textField.resignFirstResponder()
return false
}
func textFieldDidEndEditing(_ textField: UITextField) {
guard let text = textField.text else {
return
}
respondToQuestion(text)
}
#IBAction func editingChanged(_ textField: UITextField) {
guard let text = textField.text else {
askButton.isEnabled = false
return
}
askButton.isEnabled = !text.isEmpty
}
}
struct MyQuestionAnswerer {
func responseTo(question: String) -> String {
let loweredQuestion = question.lowercased()
if loweredQuestion == "What is the current vaccination rate of the Philippines?" {
return "As of August 8, the vaccination rate of the Philippines is 10%!"
} else if loweredQuestion.hasPrefix("Where") {
return "Check the map for nearby vaccination centers."
}
}
The placeholder is not present when the textFiled as it is not set until the respondToQuestion method is called. It should probably be set inside of a view controller life cycle method such as viewDidLoad().
Example:
override func viewDidLoad() {
super.viewDidLoad()
questionField.placeholder = "Ask another question"
questionField.becomeFirstResponder()
}
I attempted to bind a isEditing variable to under my UIViewRepresentable which is controlled by a close button in my SwiftUI View.
Under the UIViewRepresentable, I create a UITextfield and what I want to accomplish here is to tap the close button which triggers the isEditing variable and reset the UITextfield to make it leave edit mode. I tried to detect this change under the updateUIView
struct SearchBarViewController: UIViewRepresentable {
let searchEngine = SearchEngine()
let textField = LeftPaddedTextField(frame: .zero)
#Binding var text: String
#Binding var searchArray:[String]
#Binding var isEditing: Bool
func makeUIView(context: UIViewRepresentableContext<SearchBarViewController>) -> UITextField {
textField.delegate = context.coordinator
textField.textColor = UIColor.gray
textField.placeholder = "Where are you going?"
textField.layer.cornerRadius = 20
//textField.layer.borderWidth = 1.5
textField.layer.borderColor = UIColor.tertiaryLabel.cgColor
textField.backgroundColor = UIColor.systemGray6
textField.borderStyle = .none
textField.addTarget(context.coordinator, action: #selector(context.coordinator.textFieldDidChange), for: .editingChanged)
textField.clearButtonMode = .whileEditing
searchEngine.delegate = context.coordinator
return textField
}
func updateUIView(_ uiViewController: UITextField, context: UIViewRepresentableContext<SearchBarViewController>) {
if isEditing {
return
}
print("update is called")
if !isEditing {
//textField.resignFirstResponder()
textField.endEditing(true)
}
}
func makeCoordinator() -> SearchBarViewController.Coordinator {
Coordinator(self)
}
final class Coordinator: NSObject, UITextFieldDelegate, SearchEngineDelegate {
var control: SearchBarViewController
init(_ control: SearchBarViewController) {
self.control = control
}
func resultsUpdated(searchEngine: SearchEngine) {
self.control.searchArray = []
if !searchEngine.items.isEmpty {
for i in searchEngine.items {
if let description = i.descriptionText {
self.control.searchArray.append(description)
}
}
}
print()
}
func resolvedResult(result: SearchResult) {
print()
}
func searchErrorHappened(searchError: SearchError) {
print("Error during search: \(searchError)")
}
func textFieldDidBeginEditing(_ textField: UITextField) {
self.control.isEditing = true
}
func textFieldShouldClear(_ textField: UITextField) -> Bool {
return true
}
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
self.control.textField.resignFirstResponder()
self.control.isEditing = false
return true
}
// Update model.text when textField.text is changed
#objc func textFieldDidChange() {
if let text = self.control.textField.text {
self.control.text = text
}
if self.control.textField.text != "" {
if let text = control.textField.text {
control.searchEngine.query = text
}
} else {
self.control.searchArray = []
}
}
}
}
And here is the code of the outside view:
struct SearchBarView: View {
#State var isEditing = false
...
var body: some View {
Button(action: {
self.isEditing = false
self.text = ""
}) {
Text("Return")
}
SearchBarViewController(text: $text, searchArray: $searchArray, isEditing: $isEditing)
}
}
But the problem is it doesn't work. After I click the close button the UITextField doesn't exit its edit mode and I still can type. So I am asking whether there is a way to accomplish it.
Thanks for your help in advance.
Try to use passed in instance of text field
func updateUIView(_ uiTextField: UITextField, context: UIViewRepresentableContext<SearchBarViewController>) {
if isEditing {
return
}
print("update is called")
if !isEditing {
uiTextField.resignFirstResponder() // << here !!
// uiTextField.endEditing(true)
}
}
I have three text fields: tempTextField, airPressureTextField, and airDensityTextField. I want to create a check between Textfields to get the following logic:
If tempTextField or airPressureTextField is in focus where user trying to enter a value or user enters value and returns, I want to disable airDensityTextField. I tried this code. It disables airDensityTextField correctly. But If I remove the focus from tempTextField or airPressureTextField and empty both those two textfields, the airDensityTextField is still disabled.
func textFieldDidEndEditing(_ textField: UITextField) {
if let t = tempTextField.text, let ap = airPressureTextField.text {
airDensityTextField.isEnabled = false
} else {
airDensityTextField.isEnabled = true
}
}
I tried the code in both textFieldDidEndEditing and textFieldDidBeginEditing, the result is the same.
How can I make it work?
You can see which textfield is editing inside textFieldDidBeginEditing and disable airDensityTextField. Then, when they finish editing, you need to check if the text is "" (blank) in addition to whether it's nil or not.
class TextFieldVC: UIViewController, UITextFieldDelegate {
#IBOutlet weak var tempTextField: UITextField!
#IBOutlet weak var airPressureTextField: UITextField!
#IBOutlet weak var airDensityTextField: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
tempTextField.delegate = self
airPressureTextField.delegate = self
airDensityTextField.delegate = self
}
func textFieldDidBeginEditing(_ textField: UITextField) {
switch textField {
case tempTextField:
airDensityTextField.isEnabled = false
case airPressureTextField:
airDensityTextField.isEnabled = false
default:
break
}
}
func textFieldDidEndEditing(_ textField: UITextField) {
if
let tempText = tempTextField.text,
let apText = airPressureTextField.text,
tempText != "" || apText != "" /// check if at least one of them has text
{
airDensityTextField.isEnabled = false
} else {
airDensityTextField.isEnabled = true
}
}
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
textField.resignFirstResponder()
return false
}
}
In SwiftUI I have a simple search TextField where the user type something to be searched and a Button search.
I just want to add the option to have the button Search in the lower right corner of the Keyboard (I saw it in some application)
how can I do that?
iOS 15
You can change the return key for each textField with a simple modifier called: .submitLabel that takes the return key type and you should pass .search. Take a look at the following example:
Also, as you can see, you can have a callback to handle the return key press action just like the old textFieldShouldReturn function that is accessible by '.onSubmit' modifier.
If I understand correctly you want to change the UIReturnKeyType.
In that case you have to use UIKit since there isn't yet any option to change the type of return key in SwiftUI.
To do this, you have to make a custom TextField using UIIKit and then modify it the way you like.
Also keep in mind that the UIReturnKeyType enum is under discussion and may replace with a different implementation.
// MARK: Custom TextField
struct TextFieldTyped: UIViewRepresentable {
let keyboardType: UIKeyboardType
let returnVal: UIReturnKeyType
let tag: Int
#Binding var text: String
#Binding var isfocusAble: [Bool]
func makeUIView(context: Context) -> UITextField {
let textField = UITextField(frame: .zero)
textField.keyboardType = self.keyboardType
textField.returnKeyType = self.returnVal
textField.tag = self.tag
textField.delegate = context.coordinator
textField.autocorrectionType = .no
return textField
}
func updateUIView(_ uiView: UITextField, context: Context) {
if isfocusAble[tag] {
uiView.becomeFirstResponder()
} else {
uiView.resignFirstResponder()
}
}
func makeCoordinator() -> Coordinator {
Coordinator(self)
}
class Coordinator: NSObject, UITextFieldDelegate {
var parent: TextFieldTyped
init(_ textField: TextFieldTyped) {
self.parent = textField
}
func updatefocus(textfield: UITextField) {
textfield.becomeFirstResponder()
}
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
if parent.tag == 0 {
parent.isfocusAble = [false, true]
parent.text = textField.text ?? ""
} else if parent.tag == 1 {
parent.isfocusAble = [false, false]
parent.text = textField.text ?? ""
}
return true
}
}
}
And you can use it like this:
(Change the returnVal to .search in your case.)
struct CustomeKT: View {
#State var myTextForTX = ""
#State var focused: [Bool] = [false, true]
var body: some View {
TextFieldTyped(keyboardType: .default, returnVal: .search, tag: 0, text: self.$myTextForTX, isfocusAble: self.$focused)
}
}
Simple Use SearchTextField.swift
import SwiftUI
import UIKit
class UIKitTextField: UITextField, UITextFieldDelegate {
required init(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)!
delegate = self
}
required override init(frame: CGRect) {
super.init(frame: frame)
delegate = self
self.setContentHuggingPriority(.defaultHigh, for: .vertical)
}
var action:(() -> Void)? = nil
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
self.action?()
if let nextField = textField.superview?.viewWithTag(textField.tag + 1) as? UITextField {
nextField.becomeFirstResponder()
} else {
textField.resignFirstResponder()
return true;
}
return false
}
}
struct SearchTextField : UIViewRepresentable {
#Binding var text: String
var action:() -> Void
func makeCoordinator() -> SearchTextField.Coordinator {
return Coordinator(value: self)
}
class Coordinator: NSObject,UITextFieldDelegate {
var parent:SearchTextField
init(value: SearchTextField) {
self.parent = value
}
#objc func textFieldEditingChanged(_ sender: UIKitTextField) {
self.parent.text = sender.text ?? ""
}
}
func makeUIView(context: Context) -> UIKitTextField {
let textfield = UIKitTextField(frame: .zero)
textfield.addTarget(context.coordinator, action: #selector(Coordinator.textFieldEditingChanged(_:)), for: .editingChanged)
textfield.text = self.text
textfield.placeholder = "search"
textfield.borderStyle = .none
textfield.returnKeyType = .search
textfield.action = self.action
return textfield
}
func updateUIView(_ uiView: UIKitTextField,
context: Context) {
uiView.text = self.text
}
}
Use :
SearchTextField(text: self.$searchKey) {
self.search(key: self.searchKey)
// or...
}
I am trying to have my inputs inside of my UITextField show up in the debugger console, when I am typing in the created TextField however the Delegate Methods don't seem to be responding. I am expecting to see my print statement that are seen below for my UIdelegate methods, like when I first started typing, while I type, and when I press the 'return key'. All delegate methods do not seem to be activated, and I am not sure how to make my Textfield link to the delegate method directly. In addition, I have another UITextField (Not shown here), would I have to 'addTarget' to differentiate between the two?
class ViewController: UIViewController, UITextFieldDelegate {
let createUserName: UITextField = {
var myTextField = UITextField ()
myTextField.translatesAutoresizingMaskIntoConstraints = false
myTextField.placeholder = "Username" //set placeholder text
myTextField.font = UIFont.systemFont(ofSize: 14) // set font size of text field
myTextField.layer.borderWidth = 1.0 //set width
myTextField.layer.borderColor = UIColor.red.cgColor//set background color to a ui color
myTextField.layer.backgroundColor = UIColor.white.cgColor
myTextField.layer.cornerRadius = myTextField.frame.height/2
myTextField.autocorrectionType = .no // disable autocorrect when typing for .no, enable with .yes
myTextField.isSecureTextEntry = false// masked text
myTextField.keyboardType = .default //keyboard style is set to default
myTextField.returnKeyType = .default //retuen key text changed to "Done" instead of return
myTextField.clearButtonMode = .whileEditing
myTextField.delegate = self as? UITextFieldDelegate
return myTextField
}()
override func viewDidLoad() {
super.viewDidLoad()
createUserName.delegate = self
view.addSubview(createUserName)
setupUserName()
}
//UITextField Delegate methods
func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool {
print("textfield should begin editting")
return true
}
func textFieldDidBeginEditing(_ textField: UITextField) {
print("text field edit")
}
//see string that is typed in debugger for use to validate password and crossreference username
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
if let textFieldString = textField.text, let swtRange = Range(range, in: textFieldString) {
let fullString = textFieldString.replacingCharacters(in: swtRange, with: string)
print("FullString: \(fullString)")
}
return true
}
//dismiss keyboard when return button is pressed
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
textField.resignFirstResponder()
print("text field return")
return true
}
}
Your viewController should inherit from UITextFieldDelegate
class YourViewController : UIViewController, UITextFieldDelegate {
// your code
}
Also in your ViewDidLoad, move your createUsername.delegate = self to last line.
That string:
myTextField.delegate = self as? UITextFieldDelegate
tell us that your VC don't directly conform protocol UITextFieldDelegate...
If you conformed swift doesn't add as? cast ...