iPhone - Problem with UITextView

前端 未结 4 2079
长情又很酷
长情又很酷 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: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];

提交回复
热议问题