Jak dodać przycisk Gotowe na klawiaturze?

Aktualizacja:

Próbowałem również zaimplementować uitextviewdelegate delegate, a następnie wykonać w moim kontrolerze:

- (BOOL)textViewShouldEndEditing:(UITextView *)textView
{
    [textView resignFirstResponder];
    return YES;
}

Ustawiłem również delegat widoku tekstowego jako self (instancja widoku kontrolera).

Kliknięcie przycisku Gotowe nadal wstawia tylko nową linię : (


Aktualizacja:

To, co zrobiłem do tej pory. Zaimplementowałem UITextFieldDelegate przez mój kontroler widoku.

Połączyłem widok tekstowy z kontrolerem widoku poprzez outlet.

Potem zrobiłem:


self.myTextView.delegate = self;

I:

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];
    return YES;
}

Ale kiedy klikam na przycisk Gotowe, po prostu dodaje nową linię.

Więc mam element UITextView na mojej scenie i kiedy użytkownik dotknie go, pojawia się klawiatura i można go edytować.

Nie mogę jednak odrzucić klawiatury.

Jak dodać przycisk Gotowe do klawiatury, aby można było go usunąć?

Author: GingerHead, 2012-04-09

9 answers

To całkiem proste:)

[textField setReturnKeyType:UIReturnKeyDone];

Aby usunąć klawiaturę zaimplementuj Protokół <UITextFieldDelegate> w swojej klasie, Ustaw

textfield.delegate = self;

I używać

- (void)textFieldDidEndEditing:(UITextField *)textField {
    [textField resignFirstResponder];
}

Lub

- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    [textField resignFirstResponder];
    return YES;
}
 111
Author: Sebastian Flückiger,
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
2012-08-20 05:29:34

Wejdź do swojego storyboardu, wybierz swoje pole tekstowe i pod Inspektorem atrybutów znajduje się opcja z napisem "return key"...wybierz "Gotowe".

Następnie przejdź do kontrolera ViewController i dodaj:

- (IBAction)dismissKeyboard:(id)sender;
{
    [textField becomeFirstResponder];
    [textField resignFirstResponder];
}

Następnie wróć do pola tekstowego, kliknij outlets i link "Did end on exit", aby usunąć działanie.

 15
Author: Mike Z,
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
2012-04-09 17:38:52

Dodaj UIToolBar jako niestandardowy widok, który zrobi przycisk jako UIBarButtonItem w nim.

jest to bezpieczniejszy i czystszy sposób na dodanie przycisku Gotowe do dowolnego rodzaju klawiatury. Utwórz UIToolBar Dodaj do niego przycisk Gotowe i ustaw inputAccessoryView dowolnego UITextField lub UITextView.

UIToolbar *ViewForDoneButtonOnKeyboard = [[UIToolbar alloc] init];
[ViewForDoneButtonOnKeyboard sizeToFit];
UIBarButtonItem *btnDoneOnKeyboard = [[UIBarButtonItem alloc] initWithTitle:@"Done"
                                                               style:UIBarButtonItemStyleBordered target:self
                                                              action:@selector(doneBtnFromKeyboardClicked:)];
[ViewForDoneButtonOnKeyboard setItems:[NSArray arrayWithObjects:btnDoneOnKeyboard, nil]];

myTextField.inputAccessoryView = ViewForDoneButtonOnKeyboard;

IBAction For Done Button

 - (IBAction)doneBtnFromKeyboardClicked:(id)sender
  {
      NSLog(@"Done Button Clicked.");

      //Hide Keyboard by endEditing or Anything you want.
      [self.view endEditing:YES];
  }

SWIFT 3

var ViewForDoneButtonOnKeyboard = UIToolbar()
ViewForDoneButtonOnKeyboard.sizeToFit()
var btnDoneOnKeyboard = UIBarButtonItem(title: "Done", style: .bordered, target: self, action: #selector(self.doneBtnFromKeyboardClicked))
ViewForDoneButtonOnKeyboard.items = [btnDoneOnKeyboard]
myTextField.inputAccessoryView = ViewForDoneButtonOnKeyboard

Function

  @IBAction func doneBtnFromKeyboardClicked (sender: Any) {
     print("Done Button Clicked.")
    //Hide Keyboard by endEditing or Anything you want.
    self.view.endEditing(true)
  }
 11
Author: ViJay Avhad,
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-02-28 08:00:02

Wersja Swift:

In Your ViewController:

textField.returnKeyType = UIReturnKeyType.Done
textField.delegate = self

Po Twoim Viewcontrollerze

extension MyViewController: UITextFieldDelegate {        
    func textFieldShouldReturn(textField: UITextField) -> Bool {
        textField.resignFirstResponder()
        return true
    }
}
 9
Author: Esqarrouth,
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-10-06 20:43:27

Ustaw opcję 'Return Key' pod opcją text field w Inspektorze dla Twojego pola tekstowego. Następnie kliknij prawym przyciskiem myszy na polu tekstowym i przytrzymując CTRL Wybierz 'Did End On Exit' i przeciągnij to do pliku view controllers. Spowoduje to utworzenie metody IBAction. Nadaj metodzie nazwę, a następnie wprowadź następujące wartości:

[textField becomeFirstResponder];
[textField resignFirstResponder];

Twoja metoda powinna wyglądać tak:

- (IBAction)methodName:(id)sender;
{
    [sender becomeFirstResponder];
    [sender resignFirstResponder];
}
 1
Author: Garreth Golding,
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
2013-12-11 02:32:15

Ok, więc ja też borykam się z tym problemem. Obecnie uzyskuję dostęp do UITextView w UITableViewCell (poprzez tagi). Ponieważ używam komórek prototypowych, nie mogę używać IBActions ani Iboutlets, jak każdy sugeruje. Zamiast tego używam;

-(BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replementtext:(NSString *)text

To wyświetli mi tekst za każdym razem, gdy użytkownik naciśnie przycisk. Moim rozwiązaniem było wówczas pobiera znak ascii dla nowej linii; "/ n". Jeśli to jest tekst, który został wpisany, zrezygnowałbym z pierwszej odpowiedzi. Na przykład;

// If the text length is 0 then the user is deleting something, so only check the ascii character if there is text to check
if (text.length != 0) {
    // Get the Ascii character
    int asciiCode = [text characterAtIndex:0];
    // If the ascii code is /n or new line, then resign first responder
    if (asciiCode == 10) {
        [alertTextView resignFirstResponder];
        [DeviceTextView resignFirstResponder];
    }
}

Nie jestem pewien, czy ktoś jeszcze będzie potrzebował tego hacky rozwiązanie, ale pomyślałem, że umieścić go tam na wypadek, gdyby ktoś go potrzebuje!

 1
Author: Donato Perconti,
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
2014-01-30 00:59:19

Jest to najlepszy sposób, aby dodać gotowe przycisk na klawiaturze

textField.returnKeyType = UIReturnKeyDone;
 0
Author: DURGESH Chaurasiya,
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
2016-07-04 12:47:35

Oto moje podejście, w Swift 3. Po kliknięciu doneBtn, Niech wyśle Zdarzenie .editingDidEndOnExit. Używam tego zdarzenia, aby poradzić sobie z problemem skupienia między wieloma polami tekstowymi.

// My customized UITextField
class MyTextField: UITextField {

    override func awakeFromNib() {
        super.awakeFromNib()

        // Add toolBar when keyboardType in this set. 
        let set : [UIKeyboardType] = [.numberPad, .phonePad]
        if (set.contains(self.keyboardType) ) {
            self.addDoneToolbar()
        }
    }

    // Add a toolbar with a `Done` button
    func addDoneToolbar() {

        let toolbar = UIToolbar()
        let space = UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: self, action: nil)
        let doneBtn = UIBarButtonItem(barButtonSystemItem: .done, target: self, action: #selector(onToolBarDone))

        toolbar.items = [space, doneBtn]
        toolbar.sizeToFit()

        self.inputAccessoryView = toolbar
    }

    @objc func onToolBarDone() {
        // I use `editingDidEndOnExit` to simulate the `Done` behavior 
        // on the original keyboard.
        self.sendActions(for: .editingDidEndOnExit)
    }
}
 0
Author: AechoLiu,
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
2018-01-25 06:45:49

W Interface Builder na właściwościach textView można ustawić typ przycisku powrotu na gotowe.

Następnie należy sprawdzić, czy przycisk powrotu jest wciśnięty za pomocą

  - (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text

Sprawdź, czy tekst = = "/ n", a następnie wyłącz klawiaturę, rezygnując z pierwszej odpowiedzi w widoku tekstowym.

 -2
Author: Ryan Poolos,
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
2012-04-09 17:36:15