【问题标题】:Calling UIAlertView on UITextField UIControlEventEditingDidBegin dismisses keyboard在 UITextField UIControlEventEditingDidBegin 上调用 UIAlertView 会关闭键盘
【发布时间】:2015-04-30 19:52:03
【问题描述】:

我注意到一些非常奇怪的行为。我想在选择 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];

    }

}

当触摸 textField 时,键盘确实开始向上滑动。然而,当 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];

    }

}

【问题讨论】:

  • 那么你想在你的应用中做什么?同时使用键盘和 AlertView?
  • @BC_Dlium 我想要 1:alertView 出现并且键盘根本不会向上滑动,直到 alertView 被解除或 2:键盘在 alertView 启动并停留在那里时出现。现在键盘向上滑动然后消失,它看起来确实有问题。

标签: ios objective-c uitextfield uialertview


【解决方案1】:

实现如下 UITextField 的委托方法:

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField;

在该方法中显示alert view,在该方法中返回NO,键盘将不显示。

然后实现如下 UIAlertView 的委托方法:

- (void)alertView:(UIAlertView *)alertView willDismissWithButtonIndex:(NSInteger)buttonIndex;

在这个方法中,显示键盘:

[self.addressTextField becomeFirstResponder];

【讨论】:

  • 不起作用,因为每次文本字段成为第一响应者时都会显示警报视图...
  • 您可以保留一个布尔值,并根据您的逻辑设置它的值。然后,在“textFieldShouldBeginEditing”方法中,根据该布尔值的值显示或不显示并返回 NO/YES。
  • ...无法处理多个文本字段。
  • 处理多个文本字段,可以使用UITextField的标签。
  • 没问题,我的荣幸:)
【解决方案2】:

UIAlertView 的代码移动到- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField,如果返回NO,键盘将不会出现

【讨论】:

  • 谢谢,这是解决方案的一部分!
【解决方案3】:

尝试以下方法:

@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

【讨论】:

  • 谢谢,您的解决方案与 Aneeq 的解决方案同样适用,但他先发帖,所以我给了他答案:P
猜你喜欢
  • 1970-01-01
  • 2011-06-13
  • 2015-01-17
  • 1970-01-01
  • 2017-03-28
  • 1970-01-01
  • 1970-01-01
  • 2016-06-30
  • 2011-06-11
相关资源
最近更新 更多