ここにいる iOS 開発者がこの問題に遭遇し、解決策を提案できるかどうか疑問に思っています。これには、UITextView および UIAlertView に関連する UIKeyboard の動作が含まれます。
アプリでは、UITextView をタップすると、UIKeyboard が呼び出されます。UITextView のアクセサリ ビューでボタンをタップすると、UIAlertViewStyleLoginAndPasswordInput スタイルで UIAlertView が呼び出されます。これまでのところ、すべてが良好です。
悪い点は次のとおりです。UIAlertView を閉じると、UIKeyboard がアニメートされ、UITextView が最初のレスポンダーになると元に戻ります。望ましい動作は、アニメーションをスキップすることです。UIAlertViewDelegate メソッドで [textView becomeFirstResponder] を呼び出しても、うまくいかないようです。
動作を示すサンプル プロジェクトを次に示します。関連するコード スニペットとログを以下に示します。
これについての考えは?
- (void)viewDidLoad
{
[super viewDidLoad];
UIBarButtonItem *alertButton = [[UIBarButtonItem alloc] initWithTitle:@"Alert" style:UIBarButtonItemStyleBordered target:self action:@selector(handleAlertButtonTapped:)];
UIBarButtonItem *spacer = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithTitle:@"Done" style:UIBarButtonItemStyleBordered target:self action:@selector(handleDoneButtonTapped:)];
UIToolbar *toolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0.0, 0.0, self.view.frame.size.width, 44.0)];
toolbar.barStyle = UIBarStyleBlack;
toolbar.items = @[alertButton, spacer, doneButton];
UIView *accessoryView = [[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, self.view.frame.size.width, 44.0)];
[accessoryView addSubview:toolbar];
textView.inputAccessoryView = toolbar;
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleKeyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleKeyboardDidShow:) name:UIKeyboardDidShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleKeyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleKeyboardDidHide:) name:UIKeyboardDidHideNotification object:nil];
}
- (void)handleAlertButtonTapped:(id)sender {
NSLog(@"%@", NSStringFromSelector(_cmd));
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Styled AlertView"
message:nil
delegate:self
cancelButtonTitle:@"OK"
otherButtonTitles:nil, nil];
[alertView setAlertViewStyle:UIAlertViewStyleLoginAndPasswordInput];
[alertView show];
}
- (void)handleDoneButtonTapped:(id)sender {
[textView resignFirstResponder];
}
#pragma mark -
#pragma mark TextView Delegate Methods
- (BOOL)textViewShouldEndEditing:(UITextView *)textView {
NSLog(@"%@", NSStringFromSelector(_cmd));
return YES;
}
- (void)textViewDidEndEditing:(UITextView *)textView {
NSLog(@"%@", NSStringFromSelector(_cmd));
}
#pragma mark -
#pragma mark AlertView Delegate methods
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
NSLog(@"%@", NSStringFromSelector(_cmd));
[textView becomeFirstResponder]; // Not a solution.
}
- (void)alertView:(UIAlertView *)alertView willDismissWithButtonIndex:(NSInteger)buttonIndex {
NSLog(@"%@", NSStringFromSelector(_cmd));
[textView becomeFirstResponder]; // Not a solution.
}
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex {
NSLog(@"%@", NSStringFromSelector(_cmd));
[textView becomeFirstResponder]; // Not a solution.
}
#pragma mark -
#pragma mark Keyboard Notification Methods
- (void)handleKeyboardWillShow:(NSNotification *)notification {
NSLog(@"%@", NSStringFromSelector(_cmd));
[textView becomeFirstResponder]; // Not a solution.
}
- (void)handleKeyboardDidShow:(NSNotification *)notification {
NSLog(@"%@", NSStringFromSelector(_cmd));
[textView becomeFirstResponder]; // Not a solution.
}
- (void)handleKeyboardWillHide:(NSNotification *)notification {
NSLog(@"%@", NSStringFromSelector(_cmd));
[textView becomeFirstResponder]; // Not a solution.
}
- (void)handleKeyboardDidHide:(NSNotification *)notification {
NSLog(@"%@", NSStringFromSelector(_cmd));
[textView becomeFirstResponder]; // Not a solution.
}
ログの結果は次のようになります。
ResponderTest[1228:11303] handleKeyboardWillShow:
ResponderTest[1228:11303] handleKeyboardDidShow:
ResponderTest[1228:11303] handleAlertButtonTapped:
ResponderTest[1228:11303] handleKeyboardWillShow:
ResponderTest[1228:11303] handleKeyboardDidShow:
ResponderTest[1228:11303] alertView:clickedButtonAtIndex:
ResponderTest[1228:11303] alertView:willDismissWithButtonIndex:
ResponderTest[1228:11303] handleKeyboardWillHide:
ResponderTest[1228:11303] handleKeyboardDidHide:
ResponderTest[1228:11303] handleKeyboardWillShow:
ResponderTest[1228:11303] handleKeyboardDidShow:
ResponderTest[1228:11303] handleKeyboardWillHide:
ResponderTest[1228:11303] handleKeyboardDidHide:
ResponderTest[1228:11303] handleKeyboardWillShow:
ResponderTest[1228:11303] alertView:didDismissWithButtonIndex:
ResponderTest[1228:11303] handleKeyboardDidShow: