我注意到一些非常奇怪的行为。我想在选择UITextField时显示UIAlertView:
[self.addressTextField addTarget:self action:@selector(addressTextFieldSelected) forControlEvents:UIControlEventEditingDidBegin];
调用的方法为:
- (void)addressTextFieldSelected {
if (!geoPoint) {
UIAlertView* alertView = [[UIAlertView alloc] initWithTitle:@"Alert" message:@"Text" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
[alertView show];
}
}
触摸文本字段时,键盘确实开始向上滑动。但是,当警报视图出现时,键盘将关闭。选择"确定"并关闭警报视图后,文本字段的键盘将向上滑动。
编辑
在其他人的帮助下,我创建了这项工作,尽管我对键盘首先消失有点不满意。
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
if (textField.tag == 2) {
if (!geoPoint && !justShowedGeoPointAlert) {
showingGeoPointAlert = YES;
UIAlertView* alertView = [[UIAlertView alloc] initWithTitle:@"Alert" message:@"Make sure to geotag this address." delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
[alertView show];
return NO;
} else {
justShowedGeoPointAlert = NO;
}
}
return YES;
}
- (void)alertView:(UIAlertView *)alertView willDismissWithButtonIndex:(NSInteger)buttonIndex {
if (showingGeoPointAlert) {
justShowedGeoPointAlert = YES;
showingGeoPointAlert = NO;
[self.addressTextField becomeFirstResponder];
}
}
实现 UITextField 的以下委托方法:
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField;
在此方法中显示警报视图,并在此方法中返回NO,则不会显示键盘。
然后实现以下 UIAlertView 委托方法:
- (void)alertView:(UIAlertView *)alertView willDismissWithButtonIndex:(NSInteger)buttonIndex;
在此方法中,显示键盘:
[self.addressTextField becomeFirstResponder];
将UIAlertView
的代码移动到- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
,如果返回NO
键盘将不会出现
尝试如下操作:
@interface ViewController () <UIAlertViewDelegate, UITextFieldDelegate>
@property (weak, nonatomic) IBOutlet UITextField *textField;
@property (weak, nonatomic) UITextField *selectedTextField;
@end
@implementation ViewController
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
if (textField == self.selectedTextField) {
self.selectedTextField = nil;
return YES;
}
self.selectedTextField = textField;
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Alert" message:@"Text" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alertView show];
return NO;
}
- (void)alertView:(UIAlertView *)alertView willDismissWithButtonIndex:(NSInteger)buttonIndex {
[self.selectedTextField becomeFirstResponder];
}
@end