How to add Done button to the keyboard?

前端 未结 9 1879
南笙
南笙 2020-12-23 17:23

UPDATE:

I also tried implementing UITextViewDelegate delegate and then doing in my controller:

- (BOOL)textViewShouldEndEditing:(UITextView *)textVie         


        
相关标签:
9条回答
  • 2020-12-23 17:55

    This is best way to add Done Button on keyboard

    textField.returnKeyType = UIReturnKeyDone;
    
    0 讨论(0)
  • 2020-12-23 17:56

    Ok, so I too have been struggling with this very issue. Currently I am accessing a UITextView in a UITableViewCell (via Tags). Because I am using prototype cells I cannot use IBActions nor IBOutlets like everyone suggests. Instead, I am using;

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

    This will then provide me with the text each time the user hits a button. My solution then, was to get the ascii character for new line; "/n". If that is the text that was entered, I would resign the first responder. For example;

    // 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];
        }
    }
    

    Not sure if anyone else will need this hacky solution but I figured I'd put it out there in case someone needs it!

    0 讨论(0)
  • 2020-12-23 17:58

    Swift version:

    In Your ViewController:

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

    After your ViewController

    extension MyViewController: UITextFieldDelegate {        
        func textFieldShouldReturn(textField: UITextField) -> Bool {
            textField.resignFirstResponder()
            return true
        }
    }
    
    0 讨论(0)
提交回复
热议问题