iPhone - Problem with UITextView

前端 未结 4 2034
长情又很酷
长情又很酷 2021-01-02 23:56

This is probably an easy thing to do, but I just can\'t figure it out - how do I end editing athe textview? how can I get the keyboard to disappear? or do I have to click ou

相关标签:
4条回答
  • 2021-01-03 00:18

    Very Easy:

    [myTextField resignFirstResponder]; 
    

    will do the trick.

    0 讨论(0)
  • 2021-01-03 00:22
    [yourTextField resignFirstResponder];
    

    will make the keyboard disappear and editing end.

    0 讨论(0)
  • 2021-01-03 00:33

    First, a (to be honest) fairly simple question like this makes me wonder if you've tried reading the documentation, or searching on the internet.

    Searching for "Apple documentation UITextView" gives you this link to the class documentation. Similarly, here is the documentation for the UITextViewDelegate.

    Searching for "UITextView simple example" gives you this useful example.

    Searching for "UITextView dismiss keyboard", the first hit seems to answer your question exactly. (Although he dismisses the keyboard on a return key, which may not be what you want.) (Edit - it seems from your second comment it's exactly what you want.)

    P.S. The people above are correct, if a little terse (understandably). You need to implement a UITextViewDelegate. In that delegate, if you want to hide the keyboard on a return key, implement shouldChangeTextInRange, look for a @"\n" and resign first responder if you get it. Alternatively, add a "Done editing" button to your UI, and resign first responder if the user presses it.

    0 讨论(0)
  • 2021-01-03 00:43

    One way to end editing, tapping outside the textView, is not entirely trivial. Selecting other text views or text fields or activating a navigation control will trigger...

    - (void)textViewDidEndEditing:(UITextView *)textView
    

    ...on whatever object you've designated as the textView's delegate. You can trigger this yourself by calling...

    - (BOOL)endEditing:(BOOL)force
    

    ...on the view that contains your text field.

    Suppose I have a UITextView inside a UITableViewCell (inside a UITable). I want to enable editing to end by tapping the table. I could do this:

    - (void)viewDidLoad
    {
        [super viewDidLoad];
    
        UITapGestureRecognizer *tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(didTapTable)];
        [[self tableView] addGestureRecognizer:tapRecognizer];
        [tapRecognizer release];
    }
    
    - (void)didTapTable
    {
        [[self tableView] endEditing:YES];
    }
    

    Now whenever I tap my table, I end editing. And, as others have said, in textViewDidEndEditing I should be sure to call [textView resignFirstResponder];

    0 讨论(0)
提交回复
热议问题