我开始编写类似于聊天应用程序的程序,在此期间,我遇到了这个小问题:
我开始实现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方法中。基本上,我试图识别用户何时按下消息末尾(TextView)。但是“发送”按钮也在外面...

最佳答案

- (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;
    }
}

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

10-08 07:32
查看更多