I try a very simple example. I add a text view and a button to the view of a view controller. When the button is pressed, it shows an alert view with a text field.
My question is the following:
Suppose I am editing on the text view and press the button to show the alert view. The keyboard will first dismiss (text view resigning first responder) and then appear again (text field becoming first responder). This is really annoying. I want to see if I can do something, so that the keyboard would not dismiss and stay when I switch from the text view to the text field. Thank you all.
Here are some codes of this simple example:
- (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];
}