gpt4 book ai didi

ios - UITextField InputAccessoryView 崩溃应用程序

转载 作者:行者123 更新时间:2023-11-29 03:42:13 24 4
gpt4 key购买 nike

我有一个简单的 UIViewController,其中包含一些对象:一个 UITextView 和一个作为 InputAccessoryView 添加到 UTextView 的 UIToolBar。当调用 viewDidLayoutSubviews 时,我设置一个通知观察器,以便在键盘出现时调用函数“keyboardDidShow”,这样我就可以调整 UITextView 的大小,使其不在键盘后面。当调用 textViewShouldBeginEditing 时,我将 InputAccessoryView 添加到 UITextView 中。但是当我去关闭 View 时,会抛出一个未知错误。另外,一旦我单击 UIToolBar 中充当 InputAccessoryView 的 UITextField,我就无法再返回编辑 UITextView。我的代码如下:

- (void)viewDidLayoutSubviews {
if (!resized) {
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardDidShow:) name:UIKeyboardDidShowNotification object:nil];
[poemView becomeFirstResponder];
}
}

- (void)keyboardDidShow:(NSNotification *)note {
if (!resized) {
NSDictionary *keyboardInfo = [note userInfo];
NSValue *keyboardFrameSize = [keyboardInfo valueForKey:UIKeyboardFrameBeginUserInfoKey];
CGRect keyboardFrameBeginRect = [keyboardFrameSize CGRectValue];
toolBar.frame = CGRectMake(0, 0, toolBar.frame.size.width, toolBar.frame.size.height);

[txtView setFrame:CGRectMake(txtView.frame.origin.x, txtView.frame.origin.y, txtView.frame.size.width, txtView.frame.size.height - (keyboardFrameBeginRect.size.height))];
[txtView setContentSize:CGSizeMake(txtView.frame.size.width, txtView.frame.size.height - keyboardFrameBeginRect.size.height)];
[[NSNotificationCenter defaultCenter] removeObserver:self];

resized = YES;
}
}

- (IBAction)dismissView:(id)sender {
vewTxt = nil;

[txtView setInputAccessoryView:nil];
[self dismissViewControllerAnimated:YES completion:nil];
}

- (BOOL)textViewShouldBeginEditing:(UITextView *)textView {
if (![txtView inputAccessoryView]) {
[txtView setInputAccessoryView:[self createInputAccessoryView]];
}

return YES;
}

- (UIToolbar *)createInputAccessoryView {
UIToolbar *acc = [[UIToolbar alloc] init];
[acc setBackgroundColor:[UIColor redColor]];
[acc setTintColor:[UIColor redColor]];
[acc sizeToFit];
[acc setFrame:CGRectMake(0,txtView.frame.size.height - 44, txtView.frame.size.width, 44)];

vewTxt = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, vewTxt.frame.size.width - 25, 25)];
[vewTxt setText:@"Etc...."];
[vewTxt setDelegate:self];
[vewTxt setFont:[UIFont italicSystemFontOfSize:14]];
[vewTxt setTextColor:[UIColor grayColor]];
[vewTxt setBorderStyle:UITextBorderStyleRoundedRect];

UIBarButtonItem *titleItem = [[UIBarButtonItem alloc] initWithCustomView:titleTxt];
NSArray *items = [NSArray arrayWithObjects:titleItem, nil];
[acc setItems:items animated:YES];

return acc;
}

- (void)textFieldDidBeginEditing:(UITextField *)textField {
if ([[textField text] isEqualToString:@"Etc...."]) {
[textField setTextColor:[UIColor blackColor]];
[textField setText:@""];
[textField setFont:[UIFont systemFontOfSize:14]];
}
}

最佳答案

您在创建键盘附件 View 时正在创建 TextView 吗?我不确定使用两个目的不明确的 TextView 是否明智。

我确信这是一个疏忽。您的 TextView 应该已经存在。为什么要一次又一次地创建 vewTxt?您可以设置框架,但绝对不应该创建新框架。

关于ios - UITextField InputAccessoryView 崩溃应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18260044/

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