gpt4 book ai didi

ios - UITextview 键盘不允许按 "send"按钮

转载 作者:行者123 更新时间:2023-11-29 10:42:26 25 4
gpt4 key购买 nike

我开始编写类似于聊天应用程序的程序,在此期间我遇到了这个小问题:我开始实现一个 Textview(供用户编写消息),效果很好。如果我按下 Textview,键盘就会出现,我可以写一些文本。如果用户想要发送消息,可以按下该按钮,该按钮位于 View 中。因此,如果我按下此按钮,键盘首先会淡出,并且不会发送任何消息。所以按钮的 Action 不会被调用。我希望发送按钮能够工作,即使在显示键盘时也是如此。所以如果我按下发送按钮,键盘应该向下滚动,消息应该发送到服务器。也许有人知道线索,为什么会这样。

谢谢,托拜厄斯


- (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 = @"";
}

关于ios - UITextview 键盘不允许按 "send"按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23885151/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com