gpt4 book ai didi

ios - 在不关闭键盘的情况下显示具有 UITextField 的 UIAlertController

转载 作者:行者123 更新时间:2023-11-28 17:51:41 25 4
gpt4 key购买 nike

我尝试了一个非常简单的例子。我在 View Controller 的 View 中添加了一个 TextView 和一个按钮。按下按钮时,它会显示带有文本字段的警报 View 。

我的问题如下:

假设我正在 TextView 上编辑并按下按钮以显示警报 View 。键盘将首先关闭( TextView 退出第一响应者)然后再次出现(文本字段成为第一响应者)。这真的很烦人。我想看看我是否可以做些什么,以便当我从 TextView 切换到文本字段时键盘不会消失并停留。谢谢大家。

下面是这个简单例子的一些代码:

- (void)viewDidLoad {
[super viewDidLoad];

// Text view
UITextView *textView = [[UITextView alloc] initWithFrame:CGRectMake(100, 100, 300, 300)];
textView.backgroundColor = [UIColor greenColor];
[self.view addSubview:textView];

// Button
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button.frame = CGRectMake(100,500, 200, 100);
[button setTitle:@"Show alert view" forState:UIControlStateNormal];
[button addTarget:self action:@selector(buttonPressed) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
}

-(void)buttonPressed {
UIAlertController *ac = [UIAlertController alertControllerWithTitle:@"Title" message:@"Message" preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {}];
[ac addAction:cancelAction];
[ac addTextFieldWithConfigurationHandler:^(UITextField *textField) {}];
[self presentViewController:ac animated:YES completion:nil];
}

最佳答案

基于这个答案:Keyboard hide and show again...

我认为您可以在 .m 文件的末尾添加这些行,它应该可以完成工作!

// UI AlertController Category
@interface UIAlertController (NonFirstResponder)
@end

@implementation UIAlertController (NonFirstResponder)
- (BOOL)canBecomeFirstResponder
{
return NO;
}
@end

// UIAlertAction Category
@interface UIAlertAction (NonFirstResponder)
@end

@implementation UIAlertAction (NonFirstResponder)
- (BOOL)canBecomeFirstResponder
{
return NO;
}
@end

关于ios - 在不关闭键盘的情况下显示具有 UITextField 的 UIAlertController,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31696775/

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