Errors using UITextField and UITextView - swift

I am adding to my Xcode project a text field which displays text in a text view. I have got mostly the coding for it to work, but it throws back two errors. The first one is
Cannot assign value of type '(UITextField) -> (UITextRange) -> String?' to type 'String?
This is after textView.text = mText in the enter button function. The second error is
Binary operator '+=' cannot be applied to operands of type 'String?' and '(UITextField) -> (UITextRange) -> String?'
after the textView.text += mText line. How can these problems be resolved?
import UIKit
class ShowGratitudeViewController: UIViewController {
#IBOutlet weak var textField: UITextField!
#IBOutlet weak var textView: UITextView!
#IBAction func enterTextButton(_ sender: Any) {
//gets text from text field
let mText = UITextField.text
//update previous text of textview
textView.text = mText
}
#IBAction func editTextButton(_ sender: Any) {
//gets text from text field
let mText = UITextField.text
//add text after previous text
textView.text += mText
}
}

You mistyped textField with UITextField. UITextField is a class and you can not get the text of the textField object from it.
Another error you have is += can not apply on an optional and a non-optional values. So unwrap it before applying this operator.
So the code would be like:
class ShowGratitudeViewController: UIViewController {
#IBOutlet weak var textField: UITextField!
#IBOutlet weak var textView: UITextView!
#IBAction func enterTextButton(_ sender: Any) {
//gets text from text field
let mText = textField.text
//update previous text of textview
textView.text = mText
}
#IBAction func editTextButton(_ sender: Any) {
//gets text from text field
let mText = textField.text
//add text after previous text
textView.text! += mText!
}
}

Related

How to come back in previous textfield if all textfield is empty?

[if all textfield is fill it will easily come back but if all textfield is empty and i click on last textfield then i want to come back to the previous textfield and so on how i achieved this please help me ]
import UIKit
class OTPVC: CommonViewController,UITextFieldDelegate {
#IBOutlet weak var aboutOtp: UIView!
#IBOutlet weak var scrollView: UIScrollView!
#IBOutlet weak var bottomConstraint: NSLayoutConstraint!
#IBOutlet weak var firstOtptxt: UITextField!
#IBOutlet weak var secondOtptxt: UITextField!
#IBOutlet weak var thirdOtptxt: UITextField!
#IBOutlet weak var fourthOtptxt: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
setUpView()
firstOtptxt.delegate = self
secondOtptxt.delegate = self
thirdOtptxt.delegate = self
fourthOtptxt.delegate = self
}
private func setUpView() {
aboutOtp.makeCornerRadius(10)
}
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
if ((textField.text?.count)!) < 1 && (string.count > 0) {
if textField == firstOtptxt{
secondOtptxt.becomeFirstResponder()
}
if textField == secondOtptxt {
thirdOtptxt.becomeFirstResponder()
}
if textField == thirdOtptxt {
fourthOtptxt.becomeFirstResponder()
}
if textField == fourthOtptxt {
fourthOtptxt.becomeFirstResponder()
}
textField.text = string
return false
} else if (((textField.text?.count)!) >= 1) && (string.count == 0) {
if textField == secondOtptxt {
firstOtptxt.becomeFirstResponder()
}
if textField == thirdOtptxt {
secondOtptxt.becomeFirstResponder()
}
if textField == fourthOtptxt {
thirdOtptxt.becomeFirstResponder()
}
if textField == firstOtptxt {
firstOtptxt.becomeFirstResponder()
}
textField.text = string
return false
}
else if ((textField.text?.count)!) >= 1 {
textField.text = string
return false
}
return true
}
}
[if all textfield is fill it will easily come back but if all textfield is empty and i click on last textfield then i want to come back to the previous textfield and so on how i achieved this please help me ]
[if all textfield is fill it will easily come back but if all textfield is empty and i click on last textfield then i want to come back to the previous textfield and so on how i achieved this please help me ]
protocol MyTextFieldDelegate: UITextFieldDelegate {
func textField(_ textField: UITextField, didDeleteBackwardAnd wasEmpty: Bool)
}
class MyTextField: UITextField {
override func deleteBackward() {
// see if text was empty
let wasEmpty = text == nil || text! == ""
// then perform normal behavior
super.deleteBackward()
// now, notify delegate (if existent)
(delegate as? MyTextFieldDelegate)?.textField(self, didDeleteBackwardAnd: wasEmpty)
}
}
[just put a delegate method and its work fine in my project yippy]
there is a trick, you can have a 'space' in your textfields, so that problem will be solved .

How start to UITextView from paragraph in Swift

How to start a new line in UITextView with a little space, called paragraph I believe?
What you need to do is make the controller confront to UITextFieldDelegate
for example:
class ViewController: UIViewController,UITextFieldDelegate
{
//link the textfield
#IBOutlet weak var textField: UITextField!
override func viewDidLoad()
{
super.viewDidLoad()
// link the delegate to the textfield
textField.delegate = self
}
//this function detects the return button when it's pressed
func textFieldShouldReturn(_ textField: UITextField) -> Bool
{
//since we always want to start a new line with space we override the default function
textField.text += "\n "
return false
}

Swift word processing

I'm making a simple swift application. Right now I have an input text field that takes a user input and outputs it to the output user field when a button is clicked. As shown in the code below:
import UIKit
import Foundation
class ViewController: UIViewController {
#IBOutlet weak var userNameField: UITextField!
#IBOutlet weak var textView: UITextView!
override func viewDidLoad() {
super.viewDidLoad()
userNameField.delegate = self
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
#IBAction func enterTapped(_ sender: Any) {
textView.text = "\(userNameField.text!)"
}
}
extension ViewController : UITextFieldDelegate {
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
textField.resignFirstResponder()
return true
}
}
I wanted to perform some word processing before the text is displayed to the user but, every time I try coding I get a bunch of errors. Where in the code should my word processing code go. When I refer to word processing I'm referring to some string commands such as: split, combine...
It should be here
#IBAction func enterTapped(_ sender: Any) {
let str = userNameField.text!
// do what you want with str
let addComma = str + ","
textView.text = addComma
}
Where in the code should my word processing code go.
you want to do some processing and put the processed text in the out-filed. As this process needs to take place on button action. I understand the processing should go in following method:
#IBAction func enterTapped(_ sender: Any) {
// this is the correct place for processing your string.
let inputStr = userNameField.text!
//Process 'inputStr" as needed.
textView.text = inputStr
}

textfield not empty show save button in Swift

My save button is hidden with the function saveBtnHidden() in the code below. However, the save button won't reappear when text is typed into the text field. I have tried multiple solutions similar to this. Every time that I type into the text field, the save button just won't show up.
import UIKit
class TableViewController: UITableViewController, UITextFieldDelegate {
#IBOutlet weak var saveBtn: UIButton!
#IBOutlet var nicknameField: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
saveBtnHidden()
}
func saveBtnHidden() {
if (nicknameField.text?.isEmpty ?? true) {
// is empty
saveBtn.isHidden = true
} else {
saveBtn.isHidden = false
}
}
#IBAction func saveBtnPressed(_ sender: Any) {
performSegue(withIdentifier: "nextPage", sender: nil)
}
}
You are getting this error because your function saveBtnHidden() is only called once in viewDidLoad(). It does not get called again when the text in your text field changes. To detect when text changes, you will need to add a target to your text field that calls a function when it changes (.editingChanged) like this:
nicknameField.addTarget(self, action: #selector(textFieldDidChange(_:)), for: .editingChanged)
Then in the textFieldDidChange call your saveBtnHidden() function:
func textFieldDidChange(_ textField: UITextField) {
saveBtnHidden()
}
Code adapted from: How do I check when a UITextField changes?
Use delegate to be notify of any change. Delegates is a key part of iOS development and Apple's framework.
class TableViewController: UITableViewController, UITextFieldDelegate {
#IBOutlet weak var saveBtn: UIButton!
#IBOutlet var nicknameField: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
saveBtnHidden()
nicknameField.delegate = self
}
func textFieldDidChange(_ textField: UITextField) {
saveBtnHidden()
}
// More of your source code below...

Converting UITextField to String in Swift

I'm trying to convert a UITextField object to a String so that I can pass it to a function in my model file. But it is printing a blank even after I type in some characters in the name input text field and then click outside of it. Why is nothing stored there?
In view controller file:
#IBOutlet weak var nameInputText: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
nameInputText.delegate = self
let text: String = nameInputText.text!
print(text) //this prints a blank
model.validateName(nametext: text)
}
It should be like below
#IBOutlet weak var usernameInputText : UITextField!
override func viewDidLoad() {
super.viewDidLoad()
usernameInputText.delegate = self
}
func ClickOnButton()
{
let text: String = usernameInputText.text!
print(text) //this prints My text
model.validateName(nametext: text)
}
func textField(_ textField: UITextField,
shouldChangeCharactersIn range: NSRange,
replacementString string: String)
-> Bool
{
let text: String = usernameInputText.text!
print(text) //this prints My text
model.validateName(nametext: text)
}
viewDidLoad gets called before your ViewController would be visible to the user, so any text field can only contain text if you have set that text before (in InterfaceBuilder or code). If you want to access user input from that text field, you should either do that in textFieldDidEndEditing or in any other function which you know can only be called after the user interaction happened.
#IBOutlet weak var nameInputText: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
nameInputText.delegate = self
}
extension ViewController: UITextFieldDelegate {
func textFieldDidEndEditing(_ textField: UITextField) {
model.validateName(nametext: textField.text)
}
}