Check for changes in UITextField - ios

I'm attempting to make an app that checks the text in a UITextField and does something in response to what is written in it. I could possibly have the user type their input and then press a button. I want to be able to check the text field constantly without the application becoming unresponsive.

What you have to do is to attach an IBAction to your UITextField Sent Events Editing Changed:
import UIKit
class ViewController: UIViewController {
#IBOutlet weak var strLabel: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
#IBAction func editingChanged(sender: UITextField) {
strLabel.text = sender.text
}
}

Related

How to change label text according to TextField content while edited

I've just approached Swift and I'm trying to learn some very basic stuff.
I'm trying to achieve this:
I have a TextField and an Label, and I want the Label to be set to a specific emoji, according to the TextField text (which should a country name).
Example:
If the TextField contains the word "Italy", I want the Label to be set to "🍕".
If the TextField contains the word "Mexico", the Label should be set to "🌮".
This should happen while the TextField is being edited, not using any button to confirm the text in it.
This is my (not working, obviously) code.
#IBOutlet weak var emojiLabel: UILabel!
#IBOutlet weak var myTestField: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
changeLabel()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func changeLabel() {
emojiLabel.text = "🌎"
var countryName = myTestField.text
if (countryName == "Italia") {
emojiLabel.text = "🍕"
print (emojiLabel)
}
}
The first thing you want to do is set up a dictionary with all of the pairs you want, for example:
//Put this line outside of viewDidLoad
var dictionary: [String: String] = [:]
//Put these lines inside of viewDidLoad
dictionary["Italy"] = "🇮🇹"
dictionary["Mexico"] = "🌮"
//...Add more pairs to the dictionary
Now that you have defined all of your pairs, the next thing you want to do is setup the delegate of your text field. Doing this allows you to run some block of code whenever text is entered into your textfield. First, in your view controllers class definition, you want to conform to the UITextFieldDelegate:
class ViewController: UIViewController, UITextFieldDelegate {
//...rest of the code in the view controller
}
Then, in your viewDidLoad, you want to set the delegate of your text field to self (a.k.a. to this view controller, which we have just conformed to the UITextFieldDelegate):
override func viewDidLoad() {
super.viewDidLoad()
//...rest of code in viewDidLoad
myTestField.delegate = self
}
Now, with all of that out of the way, we want to call this function, just start typing textfield and it will autocomplete because we have already conformed to the UITextFieldDelegate:
func textFieldDidEndEditing(_ textField: UITextField) {
//...This function is called EVERY time editing is finished in myTestField
}
Ok, so inside of the textFieldDidEndEditing function, all we need to do is check what was entered in myTestField and see if it matches any of the keys in the dictionary we created in the first step...if it does, then we are finally going to update our label:
func updateLabel(textField: UITextField) {
//we are using guard here because we are dealing with optional values,
//meaning that textField.text doesn't necessarily have a value, and
//dictionary[text] may not actually have a value either.
guard let text = textField.text,
let labelText = dictionary[text] else {return}
//Now we are going to actually update our label
emojiLabel.text = labelText
}
Ok, looking good, last thing to do is just call our updateLabel function inside of the textFieldDidEndEditing function:
func textFieldDidEndEditing(_ textField: UITextField) {
//...This function is called EVERY time editing is finished in myTestField
updateLabel(textField: textField)
}
That's it!
I made it thanks to Teetz comment.
This is my working code if someone else needs it:
EDIT 1: I strongly recommend to use the Greg solution, marked as answer.
#IBOutlet weak var emojiLabel: UILabel!
#IBOutlet weak var myTestField: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
myTestField.addTarget(self, action: #selector(ViewController.textFieldDidChange(_:)), for: UIControlEvents.editingChanged)
textFieldDidChange(myTestField)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
#objc func textFieldDidChange(_ textField: UITextField) {
emojiLabel.text = "🌎"
var countryName = myTestField.text
if (countryName == "Italia") {
emojiLabel.text = "🍕"
}
}

How To Prompt a New Page After Inserting Name?

I created a simple page of my app today. And, now I want to expand it.
import UIKit
class ViewController: UIViewController {
#IBOutlet weak var myLabel: UILabel!
#IBOutlet weak var myTextField: UITextField!
#IBOutlet weak var display: UILabel!
#IBAction func myButtonPressed(_ sender: Any) {
display.text = "Hi \(myTextField.text!)! What can I do for you
today?"
}
override func viewDidLoad() {
super.viewDidLoad()
}
}
I want the app to prompt a new page after the user entered their name.
You can conform to the UITextViewDelegate and present a viewController in func textViewDidEndEditing(_ textView: UITextView) like:
extension ViewController: UITextFieldDelegate {
func textViewDidEndEditing(_ textView: UITextView) {
self.present(/*the targeted view controller*/)
}
}
You can add target like
// In viewDidLoad
myTextField.addTarget(self, action: #selector(onNameChange(sender:)), for: .editingChanged)
// on value change
#objc func onNameChange(sender:UITextField) {
// Do something
}
So far I got your query as:
You want to go to new ViewController when the user is done with filling his name in the textfield.
If I got you right then choose the "GO" (or any thing you wish from options) as the Return Key value inside 'Text Input Traits Section' of Attribute Inspector.
And now add this code in your view controller class with implementing UITextFieldDelegate:
func textFieldShouldReturn(_ textField: UITextField) -> Bool // called when 'return' key pressed. return false to ignore.
{
print(textField.tag)
if (textField.text?.isEmpty)! {
//show alert that text field is empty
return false
}
/* as per your case we have only one textfield,
So there no need of switch case and you can
directly present your next vc from here without having any button on UI */
return false
}

today extension shows "unable to load" after button event (iOS)

Good morning!
I have an "unable to load" problem in my iOS widget. I've read a lot of about the "unable to load" message but nothing fixed my problem. I'm not sure but I think my problem is to refresh the widget after changing my content.
My widget has one button and one label. If the user press the button the text from the label will changed - in this moment the widget shows "unable to load". Just a milisecond after pressing the button.
import UIKit
import NotificationCenter
class TodayViewController: UIViewController, NCWidgetProviding {
#IBOutlet var segment_att: UISegmentedControl!
override func viewDidLoad() {
super.viewDidLoad()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
func widgetPerformUpdateWithCompletionHandler(completionHandler: ((NCUpdateResult) -> Void)) {
completionHandler(NCUpdateResult.NewData)
}
func widgetMarginInsetsForProposedMarginInsets(defaultMarginInsets: UIEdgeInsets) -> UIEdgeInsets {
return UIEdgeInsetsZero
}
#IBAction func button_ae(sender: AnyObject) {
let tableviewclass = TodayTableViewController()
tableviewclass.newData()
}
}
Important is that the label is shown in a TableViewCell of a TableViewController. So the TableViewController is embeded in the ViewController within a Container... The listener from the button call the method newdata() of the file of the TableViewController.
import UIKit
import NotificationCenter
class TodayTableViewController: UITableViewController, NCWidgetProviding {
#IBOutlet var table: UITableView!
#IBOutlet var label1: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
init()
}
func init() {
let meldung: String = "test"
label1.text = meldung
}
func newData() {
let meldung: String = "new test"
label1.text = meldung
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}
The code is really simple and basic - so I'm wondering about the problem in this simple mechanism. I hope you can help me!
Thanks at all!
Your code assumes that label1 has been set when newData() is called, even immediately after the constructor is called.
Try using this optional chaining syntax instead, which will quietly fail if the property is nil:
import UIKit
import NotificationCenter
class TodayTableViewController: UITableViewController, NCWidgetProviding {
#IBOutlet var table: UITableView!
#IBOutlet var label1: UILabel!
var meldung: String = "test" // <-- meldung is property
override func viewDidLoad() {
super.viewDidLoad()
init()
}
func init() {
label1?.text = melding // <-- optional chaining
}
func newData() {
melding = "new test" // <-- set the class property
label1?.text = meldung // <-- optional chaining
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}
and instead of calling newData(), you might instead just set the meldung property, e.g.:
tableviewclass.meldung = "new test"
as your viewDidLoad() will take care of setting the UILabel text from the property

How do you run a section of code when the user taps the UITextView in Swift?

So I have a UITextView and some placeholder text inside. When the user taps inside the the view, I want to execute some code, i.e. clear the placeholder text. I was trying to create an IBAction but it won't let me. I looked it up online and found this UITextViewDelegate Protocol Reference but I can't figure out how to use it. A lot of the examples I've found for working with delegates are Objective-C and I am working in Swift.
Sorry for the simple question I'm new at this.
Thanks!
Given an IBOutlet to a text view someTextView, all you need to do is make your class conform to UITextViewDelegate, set that text view's delegate to self, and implement the textViewDidBeginEditing method:
class ViewController: UIViewController, UITextViewDelegate {
#IBOutlet weak var someTextView: UITextView!
override func viewDidLoad() {
super.viewDidLoad()
someTextView.delegate = self
}
func textViewDidBeginEditing(textView: UITextView) {
println("Some code")
}
}
The View Controller should adhere to UITextViewDelegate. Then make sure to implement textViewDidBeginEditing delegate methods. The below code should clear the default place holder text when the user starts editing the textview.
import UIKit
class ViewController: UIViewController, UITextViewDelegate {
#IBOutlet weak var textView: UITextView!
override func viewDidLoad() {
super.viewDidLoad()
self.textView.delegate = self
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func textViewDidBeginEditing(textView: UITextView) {
self.textView.text = ""
}
}

Printing input from TextField to a Label in Xcode with Swift

I'm working on a simple guessing game app, just to get more comfortable with Swift and Xcode. I have been able to input within userInput and get it to print a message to the console, however when I try to get it to print my input to usersGuess(which is a label), I can not figure it out.
Here's my code within a single view application via Xcode:
import UIKit
class ViewController: UIViewController {
#IBOutlet weak var correctAnswerLabel: UILabel!
#IBOutlet weak var usersGuess: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
#IBAction func buttonPressed() {
correctAnswerLabel.text = "Changes when the button is pressed."
}
#IBAction func userInput(sender: UITextField) {
println("This is working")
}
}
I'm sure this is simple, but I am scratching my head lol.
#IBAction func userInput(sender: UITextField) {
println("This is working")
usersGuess.text = sender.text
}
Although I am still new to iOS dev and Swift, I think you could also take a look at the use of delegate in this tutorial Apple provides. I guess it might be the code didn't resign your text field's first-responder status. Hence, the usersGuess could not update. (Anyone who knows how this work please leave a comment.)
To do this, basically
Create an outlet for the UITextField that receives user's input, say, usersInput.
Set ViewController as a delegate of usersInput, which will
Resign the first-responder status of usersInput when the Return button on the keyboard is pressed.
Update the text of usersGuess.
Code here:
import UIKit
class ViewController: UIViewController, UITextFieldDelegate {
#IBOutlet weak var correctAnswerLabel: UILabel!
#IBOutlet weak var usersGuess: UILabel!
#IBOutlet weak var usersInput: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
// Set ViewController as a delegate
usersInput.delegate = self
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
// Here are the callback functions for usersInput
func textFieldShouldReturn(textField: UITextField) -> Bool {
textField.resignFirstResponder()
return true
}
func textFieldDidEndEditing(textField: UITextField) {
usersGuess.text = textField.text
}
#IBAction func buttonPressed() {
correctAnswerLabel.text = "Changes when the button is pressed."
}
#IBAction func userInput(sender: UITextField) {
println("This is working")
}
}

Resources