텍스트 필드가 변경되는 시점을 확인하려고하는데 textView에 사용 된 기능과 동일합니다. textViewDidChange
지금 까지이 작업을 수행했습니다.
func textFieldDidBeginEditing(textField: UITextField) {
if self.status.text == "" && self.username.text == "" {
self.topRightButton.enabled = false
} else {
self.topRightButton.enabled = true
}
}
어떤 종류의 작품이지만 topRightButton
텍스트 필드를 누르 자마자 활성화되어 있습니까? 텍스트를 실제로 입력 할 때만 활성화하고 싶습니다.
답변
빠른
스위프트 4.2
textfield.addTarget(self, action: #selector(ViewController.textFieldDidChange(_:)), for: .editingChanged)
과
@objc func textFieldDidChange(_ textField: UITextField) {
}
스위프트 3 & 스위프트 4.1
textField.addTarget(self, action: #selector(ViewController.textFieldDidChange(_:)), for: .editingChanged)
과
func textFieldDidChange(_ textField: UITextField) {
}
스위프트 2.2
textField.addTarget(self, action: #selector(ViewController.textFieldDidChange(_:)), forControlEvents: UIControlEvents.EditingChanged)
과
func textFieldDidChange(textField: UITextField) {
//your code
}
목표 -C
[textField addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
textFieldDidChange 메소드는
-(void)textFieldDidChange :(UITextField *) textField{
//your code
}
답변
인터페이스 빌더에서이 연결을 작성할 수 있습니다.
답변
스위프트 5.0
textField.addTarget(self, action: #selector(ViewController.textFieldDidChange(_:)),
for: .editingChanged)
처리 방법 :
@objc func textFieldDidChange(_ textField: UITextField) {
}
스위프트 4.0
textField.addTarget(self, action: #selector(ViewController.textFieldDidChange(_:)),
for: UIControlEvents.editingChanged)
처리 방법 :
@objc func textFieldDidChange(_ textField: UITextField) {
}
스위프트 3.0
textField.addTarget(self, action: #selector(textFieldDidChange(textField:)), for: .editingChanged)
처리 방법 :
func textFieldDidChange(textField: UITextField) {
}
답변
내가 지금까지 처리 한 방식 : UITextFieldDelegate
func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool
{
// text hasn't changed yet, you have to compute the text AFTER the edit yourself
let updatedString = (textField.text as NSString?)?.stringByReplacingCharactersInRange(range, withString: string)
// do whatever you need with this updated string (your code)
// always return true so that changes propagate
return true
}
스위프트 4 버전
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
let updatedString = (textField.text as NSString?)?.replacingCharacters(in: range, with: string)
return true
}
답변
스위프트 3
textField.addTarget(self, action: #selector(ViewController.textFieldDidChange(sender:)), for: UIControlEvents.editingChanged)
답변
스위프트 3.0.1+ (다른 신속한 3.0 답변 중 일부는 최신이 아닙니다)
textField.addTarget(self, action: #selector(ViewController.textFieldDidChange(_:)),
for: UIControlEvents.editingChanged)
func textFieldDidChange(_ textField: UITextField) {
}
답변
textField (_ : shouldChangeCharactersIn : replacementString 🙂 은 모든 키 누름을 확인하려면 Xcode 8, Swift 3에서 저에게 효과적이었습니다 .
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
// Whatever code you want to run here.
// Keep in mind that the textfield hasn't yet been updated,
// so use 'string' instead of 'textField.text' if you want to
// access the string the textfield will have after a user presses a key
var statusText = self.status.text
var usernameText = self.username.text
switch textField{
case self.status:
statusText = string
case self.username:
usernameText = string
default:
break
}
if statusText == "" && usernameText == "" {
self.topRightButton.enabled = false
} else {
self.topRightButton.enabled = true
}
//Return false if you don't want the textfield to be updated
return true
}