Ios 在UIExtField uicontrolEventEditingDidBegen上调用UIAlertView会解除键盘
我注意到一些非常奇怪的行为。我想在选择UIExtField时显示UIAlertView:Ios 在UIExtField uicontrolEventEditingDidBegen上调用UIAlertView会解除键盘,ios,objective-c,uitextfield,uialertview,Ios,Objective C,Uitextfield,Uialertview,我注意到一些非常奇怪的行为。我想在选择UIExtField时显示UIAlertView: [self.addressTextField addTarget:self action:@selector(addressTextFieldSelected) forControlEvents:UIControlEventEditingDidBegin]; 正在调用的方法是: - (void)addressTextFieldSelected { if (!geoPoint) {
[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];
}
}
触摸文本字段时,键盘确实开始向上滑动。但是,当alertView出现时,键盘将关闭。选择“确定”并取消alertView后,文本字段的键盘向上滑动
编辑
在其他人的帮助下,我创造了这项工作,尽管我有点不满意的是,键盘一开始就消失了
- (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];
}
}
将
UIAlertView
的代码移动到-(BOOL)文本字段shouldBeginediting:(UITextField*)文本字段
,如果返回NO
键盘将不会出现将UIAlertView
的代码移动到-(BOOL)文本字段shouldBeginediting:(UITextField*))text字段
如果返回否
键盘将不会出现执行以下UITextField的委托方法:
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField;
在此方法中显示警报视图,并在此方法中返回否,将不显示键盘
然后实现UIAlertView的以下委托方法:
- (void)alertView:(UIAlertView *)alertView willDismissWithButtonIndex:(NSInteger)buttonIndex;
在此方法中,显示键盘:
[self.addressTextField becomeFirstResponder];
实现UITextField的以下委托方法:
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField;
在此方法中显示警报视图,并在此方法中返回否,将不显示键盘
然后实现UIAlertView的以下委托方法:
- (void)alertView:(UIAlertView *)alertView willDismissWithButtonIndex:(NSInteger)buttonIndex;
在此方法中,显示键盘:
[self.addressTextField becomeFirstResponder];
请尝试以下操作:
@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
@界面视图控制器()
@属性(弱,非原子)IBUITEXTFIELD*textField;
@属性(弱,非原子)UITextField*selectedTextField;
@结束
@实现视图控制器
-(BOOL)textField应该开始编辑:(UITextField*)textField{
if(textField==self.selectedTextField){
self.selectedTextField=nil;
返回YES;
}
self.selectedTextField=textField;
UIAlertView*alertView=[[UIAlertView alloc]initWithTitle:@“警报”消息:@“文本”委托:自取消按钮:@“确定”其他按钮:无];
[警报视图显示];
返回否;
}
-(void)alertView:(UIAlertView*)alertView将使用buttonIndex:(NSInteger)buttonIndex解除警报{
[self.selectedTextField成为第一响应者];
}
@结束
尝试以下方法:
@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
@界面视图控制器()
@属性(弱,非原子)IBUITEXTFIELD*textField;
@属性(弱,非原子)UITextField*selectedTextField;
@结束
@实现视图控制器
-(BOOL)textField应该开始编辑:(UITextField*)textField{
if(textField==self.selectedTextField){
self.selectedTextField=nil;
返回YES;
}
self.selectedTextField=textField;
UIAlertView*alertView=[[UIAlertView alloc]initWithTitle:@“警报”消息:@“文本”委托:自取消按钮:@“确定”其他按钮:无];
[警报视图显示];
返回否;
}
-(void)alertView:(UIAlertView*)alertView将使用buttonIndex:(NSInteger)buttonIndex解除警报{
[self.selectedTextField成为第一响应者];
}
@结束
那么您希望在应用程序中添加什么?键盘和AlertView同时出现?@BC_Dlium我想要1:AlertView出现并且键盘在AlertView关闭之前不会向上滑动,或者2:当AlertView启动并停留在那里时键盘出现。现在,随着键盘向上滑动然后消失,它看起来真的很有问题。那么你想在你的应用程序中做什么呢?键盘和AlertView同时出现?@BC_Dlium我想要1:AlertView出现并且键盘在AlertView关闭之前不会向上滑动,或者2:当AlertView启动并停留在那里时键盘出现。现在,随着键盘向上滑动然后消失,它看起来真的有问题。不起作用,因为每次文本字段成为第一响应者时都会显示alertview…您可以保留一个布尔值,并根据您的逻辑设置它的值。然后,在“textFieldShouldBeginEditing”方法中,根据该布尔值显示或不显示并返回否/是…但不能处理多个文本字段。要处理多个文本字段,可以使用UITextField的标记。没问题,我的荣幸:)不起作用,因为每次文本字段成为第一响应者时都会显示alertview…您可以保留一个布尔值,并根据您的逻辑设置它的值。然后,在“textFieldShouldBeginEditing”方法中,根据布尔值显示或不显示并返回否/是…而不能处理多个文本字段。要处理多个文本字段,可以使用UITextField的标记。没问题,我很高兴:)谢谢,这是解决方案的一部分!谢谢,这是解决方案的一部分!谢谢,你的解决方案对Aneeq同样有效,但他先发布了,所以我给了他答案:P Hanks,你的解决方案对Aneeq同样有效,但他先发布了,所以我给了他答案:P