jak dodać akcję na kluczu zwrotnym UITextField?
Mam przycisk i text textfield w moim widoku. po kliknięciu na pole tekstowe pojawia się klawiatura i mogę pisać na polu tekstowym, a także mogę odrzucić klawiaturę, klikając przycisk dodając:
[self.inputText resignFirstResponder];
Teraz chcę włączyć klawisz return klawiatury. kiedy nacisnę klawiaturę klawiatura zniknie i coś się stanie. Jak mogę to zrobić?
74
2 answers
Upewnij się, że "self" subskrybuje UITextFieldDelegate
i zainicjalizuj inputText za pomocą:
self.inputText.delegate = self;
Dodaj następującą metodę do "self":
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
if (textField == self.inputText) {
[textField resignFirstResponder];
return NO;
}
return YES;
}
Lub w języku Swift:
func textFieldShouldReturn(textField: UITextField) -> Bool {
if textField == inputText {
textField.resignFirstResponder()
return false
}
return true
}
157
Author: Ander,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-12-31 05:15:18
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-12-31 05:15:18
Z rozszerzeniem style w swift 3.0
Najpierw skonfiguruj delegata dla swojego pola tekstowego.
override func viewDidLoad() {
super.viewDidLoad()
self.inputText.delegate = self
}
Następnie dostosuj do UITextFieldDelegate
w rozszerzeniu kontrolera widoku
extension YourViewController: UITextFieldDelegate {
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
if textField == inputText {
textField.resignFirstResponder()
return false
}
return true
}
}
6
Author: Fangming,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-07-14 15:44:35
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-07-14 15:44:35