UITextview键盘不允许按“发送”按钮

时间:2014-05-27 09:00:57

标签: ios keyboard textview

我开始编写类似聊天应用程序的东西,在此期间我遇到了这个小问题: 我开始实现Textview(用户编写消息),工作正常。如果我按下Textview,键盘会出现,我可以写一些文字。如果用户想要发送消息,用户可以按下的按钮位于视图中。因此,如果我按下此按钮,键盘将首先淡出,并且不会发送任何消息。因此不会调用按钮的动作。我希望发送按钮工作,即使显示键盘。因此,如果我按下发送按钮,键盘应向下滚动,并将消息发送到服务器。 也许有人知道一个线索,为什么会发生这种情况。

谢谢, 托拜厄斯


- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {

    UITouch *touch = [[event allTouches] anyObject];
    if ([messagesend isFirstResponder] && [touch view] != messagesend) {
        [messagesend resignFirstResponder];
    }
    [super touchesBegan:touches withEvent:event];
}

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

    if([text isEqualToString:@"\n"]){
            [textView resignFirstResponder];
            textView.contentOffset = CGPointMake(0.0, textView.contentSize.height-textView.frame.size.height);

        }
    return YES;
}

- (void) keyboardDidShow:(NSNotification *)note {
    NSDictionary *userInfo = [note userInfo];
    CGSize kbSize = [[userInfo objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;

    CGRect frame = messagesendview.frame;
    frame.origin.y = frame.origin.y-(kbSize.height);

    CGRect frame1 = _myTableView.frame;
    frame1.size.height = frame1.size.height-(kbSize.height);

    NSNumber *durationValue = userInfo[UIKeyboardAnimationDurationUserInfoKey];
    NSTimeInterval animationDuration = durationValue.doubleValue;


    [UIView animateWithDuration:animationDuration animations:^{
        messagesendview.frame = frame;
        _myTableView.frame = frame1;
    }];

    if (_myTableView.contentSize.height > _myTableView.frame.size.height)
    {
        CGPoint offset = CGPointMake(0, _myTableView.contentSize.height - _myTableView.frame.size.height);
        [self.myTableView setContentOffset:offset animated:YES];
    }
}

- (void)keyboardDidHide:(NSNotification *)note
{

    NSDictionary *userInfo = [note userInfo];
    CGSize kbSize = [[userInfo objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;

    CGRect frame = messagesendview.frame;
    frame.origin.y = frame.origin.y+(kbSize.height);

    NSNumber *durationValue = userInfo[UIKeyboardAnimationDurationUserInfoKey];
    NSTimeInterval animationDuration = durationValue.doubleValue;

    CGRect frame1 = _myTableView.frame;
    frame1.size.height = frame1.size.height+(kbSize.height);


    [UIView animateWithDuration:animationDuration animations:^{
        messagesendview.frame = frame;
        _myTableView.frame = frame1;
    }];

}
- (IBAction)send:(id)sender {
    [messagesend resignFirstResponder];
    [self sendmessage];
    }

我认为我的问题出在touchesBegan方法中。基本上我试图识别用户何时按下messagesend(TextView)。但是Button" Send"在外面也是......

3 个答案:

答案 0 :(得分:2)

- (BOOL) textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
    if([text isEqualToString:@"\n"]){
        [textView resignFirstResponder];
        //Here u can call your "send message" method
        return NO;
    }else{
        return YES;
    }
}

OR:

- (IBAction)yourSendButton:(id)sender {
    [self.yourTextView resignFirstResponder];
    [self sendMessage:self.yourTextView.text];
    self.yourTextView.text = @"";
}

答案 1 :(得分:0)

UITextView.

的委托方法中写下发送消息的代码
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {

  if([text isEqualToString:@"\n"])
  {
   /// Call method OR write code of send message
    return NO;
  }
  else
    return YES;
}

答案 2 :(得分:0)

我希望你很好。

问题是我遇到了这个问题,需要时间来找出最佳解决方案。

UITextView没有像UITextField那样响应sendButton事件:

func textFieldShouldReturn(_ textField: UITextField) -> Bool {
    if textField.returnKeyType == UIReturnKeyType.send {

        //--> here to delegate the function of the sending
    }
    return true
}

因此,如果您也想使用“发送”按钮,请执行 textView.returnKeyType == UIReturnKeyType.send

之后

func textViewDidChange(_ textView:UITextView){

//如果用户点击将导致换行符的返回按钮(或发送按钮)         if textView.text.contains(" \ n"){

        //clear text and resignfirstresponder
        textView.text = ""
        textView.resignFirstResponder()
    }
}

贝斯茨,