【发布时间】:2015-10-20 05:12:40
【问题描述】:
我尝试了一个非常简单的例子。我在视图控制器的视图中添加了一个文本视图和一个按钮。当按下按钮时,它会显示一个带有文本字段的警报视图。
我的问题如下:
假设我正在文本视图上进行编辑并按下按钮以显示警报视图。键盘将首先关闭(文本视图退出第一响应者)然后再次出现(文本字段成为第一响应者)。这真的很烦人。我想看看我能不能做点什么,这样当我从文本视图切换到文本字段时,键盘就不会关闭并停留。谢谢大家。
下面是这个简单例子的一些代码:
- (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];
}
【问题讨论】:
-
我不认为你可以这样做,除非你使用自定义键盘
标签: ios keyboard textview uitextview uialertcontroller