TextField Action After Done

I want my textfield after I click done on the keyboard to move the slider. I'm not really sure how to do that though. Is there a way to do it or do I need a button?


Answered by ZoneX in 648942022
I am trying to allow the textField I have to accept an Int that moves a slider, I can get it to work by commenting out my function that only allows values from 10 to 90.


Code Block
func textFieldShouldEndEditing(_ textField: UITextField) -> Bool {
    // Make sure the user entered a number
  let num = Double(textField.text!)
  if num! < 10 {
      textField.text = "10%"
  } else if num! > 90 {
      textField.text = "90%"
  } else if num! >= 10 || num! <= 90 {
      textField.text = String(Int(num!)) + "%"
  }
  //print(num!)
  return true
}

Done button is a type of return key and you can customize the behavior by implementing textFieldShouldReturn(_):
Code Block
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var textField: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
textField.delegate = self
textField.returnKeyType = .done
}
}
extension ViewController: UITextFieldDelegate {
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
print(#function)
textField.resignFirstResponder()
//Do something...
return true
}
}


Does it have to be TextFieldShouldReturn can there be another function if I want to add another textfield?

Does it have to be TextFieldShouldReturn

It's textFieldShouldReturn and not TextFieldShouldReturn. In Swift, identifiers are case sensitive and you should better care about it.

can there be another function if I want to add another textfield?

NO. If you want to use multiple textfields, you need to write branches in textFieldShouldReturn(_:).
I mean lets say I have another textField that I want to control a different slider is there another function other then textFieldShouldReturn()?

 is there another function other then textFieldShouldReturn()?

Whatever you mean, the answer would not change.

NO. If you want to use multiple textfields, you need to write branches in textFieldShouldReturn(_:).
Okay I see what you mean but I have an issue implementing it for two textfields. It says my text value is nil and it does not move the slider.

Code Block
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
  textField.resignFirstResponder()
  let text = textField.text
  let textNum = Int(text!)
  /*when you enter a value it will affect, the slider and squareOne width value will change and squareTwo width will change */
  if widthPer.text == text {
    squareOneWidth.constant = CGFloat(textNum!) * 2
    slider.value = Float(textNum!)
    calPercentage()
    calPercentageTwo()
    /*when you enter a value it will affect, the sliderHeight and squareOne height will change and squareTwo height will change */
  } else if heightPer.text == text {
    calPercentage()
    calPercentageTwo()
  }
  print("\(text)")
  return true
}

 It says my text value is nil and it does not move the slider.

Sorry, but you use too many unexplained things.

What is my text value? What is it saying it is nil.

And in your code:
What is widthPer?
What is squareOneWidth?
What is slider?
What is calPercentage()?
What is calPercentageTwo()?
What is heightPer?

For properties of some view controller, better include the definition of them which explains what they are clearly.
If the view controller is not too big, showing the full code of the class would help.

I know there is a lot of details but I think if I got the slider at the position of what the text input is then I could solve the rest on my own. slider.value = Float(textNum!)

I know there is a lot of details

I do not mean to discuss your code in detail. I just want enough info to write a reply.

Please share your solution when you solved your issue yourself.
Thats the thing I'm struggling with. I modified the function so it can be better understood. widthPer is the textField I have selected to change my slider value when a value is entered but it does not work and the textField returns nil.

Code Block
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
  textField.resignFirstResponder()
  let text = textField.text
  let textNum = Int(text!)
  if widthPer.text == text {
    slider.value = Float(textNum!)
  }
return true
}

the textField returns nil

It does not make sense as textField in textFieldShouldReturn(_:) can never be nil.
It says found nil while unwrapping an optional value. slider.value = Float(textNum!), the textNum is nil but when I print text it's an optional value. let text = textField.text

It says found nil while unwrapping an optional value. slider.value = Float(textNum!),

If you had shown enough info, I might have been able to guess. But as you are just showing some fragments of info, it is very hard to imagine what you mean.
Use the most precise words to describe your issue, no wrong terms no ambiguous expression, if you cannot show enough info.
Accepted Answer
I am trying to allow the textField I have to accept an Int that moves a slider, I can get it to work by commenting out my function that only allows values from 10 to 90.


Code Block
func textFieldShouldEndEditing(_ textField: UITextField) -> Bool {
    // Make sure the user entered a number
  let num = Double(textField.text!)
  if num! < 10 {
      textField.text = "10%"
  } else if num! > 90 {
      textField.text = "90%"
  } else if num! >= 10 || num! <= 90 {
      textField.text = String(Int(num!)) + "%"
  }
  //print(num!)
  return true
}

I am trying to allow the textField I have to accept an Int that moves a slider, I can get it to work by commenting out my function that only allows values from 10 to 90.

Seems your new question is not related to the title and the question in your opening post.

You should better start a new thread, more readers would read it and you have more chance to get replies you expect.
TextField Action After Done
 
 
Q