Swift How to Assign a String to a Uitextfield

Swift how to assign a string to a UITextField?

resultProcessed.text = str

You need to assign it to the text property.

Cannot assign a value of type String to type UITextField

Because a UITextField is not a String. Set it's text property (which is a String) instead of the UITextField itself.

if CreatePassWord.text == pwd {
self.performSegueWithIdentifier("segue", sender: nil)

}

EDIT: Since you're likely doing comparison, not assignment, you want to compare the two strings, not assign pwd to the UITextfield.

[Swift]How to assign a string to a UITextField?

Try this,

1) Check your Outlet textfield connection
2) Then add button action for your button

 @IBAction func tapHandler(sender: UIButton) {
myTextField.text = "success"
}

Assign action to UITextField and update UILabel

Assumptions:

  • Your text field is named textField
  • Your label is named label

You can check it in that button action function like following:

    if let text = textField.text {
if text == "Specified Text" {
label.text = "Correct"
}
}

So if textField's text is equal to your specified text label's text will be "Correct", otherwise nothing will happen.

If by virtual send button you mean the return button of keyboard, you need to do the following in textFieldShouldReturn delegate method:

func textFieldShouldReturn(_ textField: UITextField) -> Bool {
if let text = textField.text {
if text == "Specified text" {
label.text = "Correct"
}
textField.text = ""
}
textField.resignFirstResponder() // this is optional, you might wanna hide keyboard or not
return true
}

Unable to set the text for UITextfield from other class

Your code is wrong.

let viewController = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "SecondViewController") as? SecondViewController
viewController?.sampleTxtField?.text = "world"

In above statment, view's not get loaded in UI so sampleTxtField always will be nil, Instead create a property in SecondVC, var sample : String? and assign it as above. Then in SecondVC's viewDidload, assign the value as textField.text = sample

// First ViewController

@IBAction func buttonAction(_ sender: Any) {
let viewController = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "SecondViewController") as? SecondViewController
viewController?.sample = "world"
if let navigator = self.navigationController {
navigator.pushViewController(viewController!, animated: true)
}
}

// Second ViewController

@IBOutlet weak var sampleTxtField: UITextField?
var sample : String?
override func viewDidLoad() {
super.viewDidLoad()
sampleTxtField.text = sample
print("the textfield value is,\(String(describing: sampleTxtField?.text))")
}

UITextField Programmatically won't convert into string or take any binary operators Xcode

Your error is self explanatory, you are trying to use the UITextField type as String. Here what you need to use is the .hasText property of UIKeyInput protocol which UITextField conforms to. Here's how:

guard emailTxt.hasText,
passwordTxt.hasText
else {
AlertController.showAlert(self, title: "Missing Info", message: "Please fill out all fields")
return
}
let email = emailTxt.text!
let password = passwordTxt.text!

You can force unwrap the text of UITextField because it defaults to empty String "". Even if it's set to nil it returns an empty string.

Swift: how to assign a string array to the text property of an array of UITextFields?

[UITextField] does not have a text property, so you can't use sArray2.text.

You can zip and then forEach:

zip(sArray2, sArray1).forEach { $0.0.text = $0.1 }


Related Topics



Leave a reply



Submit