I was trying to create a function in order to put keyboard away by clicking outside of the keyboard or return key within the keyboard, but unfortunately it only worked when I clicked outside of the keyboard, it doesn't work to press the return key in the keyboard.
import UIKit
class ViewController: UIViewController,UITextFieldDelegate {
#IBOutlet var numberEnter: UITextField!
#IBAction func findButton(sender: AnyObject) {
resultLabel.text = numberEnter.text
}
#IBOutlet var resultLabel: 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.
}
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
self.view.endEditing(true)
}
func textFieldShouldReturn(textField:UITextField) -> Bool{
textField.resignFirstResponder()
return true
}
}
If you haven't set the delegate for your text field you will need to do that. You can set the delegate in Interface Builder or in code. See this StackOverflow answer for an example: Text Field Should Return, is this correct for ios7?
Related
I'm learning Xcode and Swift, so I decided to go through the tutorial on the Apple Developer website. I've set up my text field to dismiss
the keyboard as the tutorial said, but rather than dismissing the
keyboard and performing the action done by
func textFieldDidEndEditing(_ textField: UITextField)
the text field just makes a new line and the keyboard doesn't dismiss.
Before typing and pressing done:
After typing and pressing enter:
Here is my code:
try this:
class MyViewController: UIViewController, UITextFieldDelegate {
#IBOutlet var textField : UITextField
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
self.textField.delegate = self
}
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
self.view.endEditing(true)
return false
}
}
Make sure that it is a UITextField and not a UITextView
try this :
class ViewController: UIViewController , UITextFieldDelegate {
#IBOutlet weak var textFieldTXT: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
textFieldTXT.delegate = self
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
self.view.endEditing(true)
}
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
textFieldTXT.resignFirstResponder()
return true
}
}
I´m currently working on a login and registration(Xcode 7, Swift2). If a user registers and types his desired username in the text field,i would like him to type at least 5 characters. So if he leaves the text field and haven´t typed in at least 5 characters, a message get´s displayed that tells him to type in at least 5 characters.
I only found how to determine the maximum amount of characters, but was not able to adjust it to my needs.
This is my current code:
import UIKit
class ViewController: UIViewController,UITextFieldDelegate {
// Mark: Properties
#IBOutlet weak var Username: UITextField!
#IBOutlet weak var Password: UITextField!
#IBOutlet weak var Status: UILabel!
#IBOutlet weak var DesiredUsername: UITextField!
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.
}
// Mark: Actions
#IBAction func CreateAccount(sender: UIButton) {
}
#IBAction func LoginButtonTapped(sender: UIButton) {
if (Username.text == "janoschvongehr" && Password.text == "test123") {
performSegueWithIdentifier("SeguetoPeople", sender: nil)
}
if (Username.text == "" || Password.text == "") {
Status.text = "Nicht alle Felder ausgefüllt"
}
self.Username.resignFirstResponder()
self.Password.resignFirstResponder()
}
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
self.view.endEditing(true)
}
func textFieldShouldReturn(textField: UITextField) -> Bool {
self.view.endEditing(true)
return true
}
}
I just started with programming, so it would be great if you could keep the answers as simple as possible.
Thank you, guys!
func textFieldShouldReturn(textField: UITextField) -> Bool {
if textField.text!.characters.count < 5 {
warningLabel.hidden = false
}
self.view.endEditing(true)
return true
}
should do the trick.
Seems like you are 90% of the way there, especially in the fact you already set a delegate for your text field.
Try doing:
func textFieldShouldReturn(textField: UITextField) -> Bool {
if ( textField.text.count < 5 )
{
// create a warning label IBOutlet and set it to hidden
//
// reveal it only upon leaving the text field when the
// length is less than 5
warningLabel.hidden = false;
}
self.view.endEditing(true)
return true
}
I am trying to move my UITextView to the upper half of the screen when the user taps it to begin editing. Then, when the user is done, the UITextView will return to the original position. When I run the code and tap the UITextView, the it moves to the new position but then immediately returns. What am I not doing to get the UITextField to remain in the new position until the user is finished editing? I am using Xcode 7.0.1 and Swift2. Thank you.
import UIKit
class ViewController: UIViewController, UITextFieldDelegate, UITextViewDelegate {
#IBOutlet var textViewField: UITextView!
override func viewDidLoad() {
super.viewDidLoad()
textViewField.layer.cornerRadius = 10
}
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
self.view.endEditing(true)
}
func textViewDidBeginEditing(textView: UITextView) {
UIView.animateWithDuration(1) { () -> Void in
self.textViewField.center = CGPointMake(self.textViewField.center.x, self.textViewField.center.y + 400)}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
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")
}
}
I have the following code to hide my keyboard when the user taps the view, but touchesBegan is not firing at all:
class LoginViewController: UIViewController, UITextFieldDelegate {
#IBOutlet var emailAddress: UITextField
#IBOutlet var password: UITextField
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
//Delegate fields
self.emailAddress.delegate = self
self.password.delegate = self
}
override func touchesBegan(touches: NSSet!, withEvent event: UIEvent!) {
self.emailAddress.resignFirstResponder()
self.password.resignFirstResponder()
}
func textFieldShouldReturn(textField: UITextField!) -> Bool{
self.emailAddress.resignFirstResponder()
self.password.resignFirstResponder()
return true;
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
This view controller is inside of a navigation controller, so not sure if it has something to do with the responder chain
Your code works just fine for me. (Though, typically I would use self.view.endEditing(YES) rather than resignFirstResponder on each text field.)
Most likely the view that you're tapping on is somehow preventing the event from being sent up the responder chain. It could be userInteractionEnabled, an alpha of 0, an override of touchesBegan which doesn't send the events up the responder chain, a gesture recognizer which is eating the touch events, etc. If you make a minimal test case which shows this problem, it'll probably become obvious which of these it is.