-1

在输入值时点击键盘的返回键在iOS 8UIAlertViewStyleSecureTextInputUIAlertView触发调用alertView:clickedButtonAtIndex: UIAlertViewDelegate

在 iOS 7UIAlertViewDelegate 中,相同的条件下不会触发相同的呼叫。

我不确定这是否是iOS 8 or iOS 7. 但是我的代码按照预期 iOS 8 停止运行。

代码示例:

    #include "MPAMyViewController.h"

    static NSString *MPAPasswordConfirmationAlertTitle = @"Please confirm your password.";
    static NSString *MPAPasswordConfirmationAlertCancelButtonTitle = @"Cancel";
    static NSString *MPAPasswordConfirmationAlertOkButtonTitle = @"OK";
    static NSString *MPAPasswordConfirmationTextFieldPlaceholder = @"Enter Password";

    @interface MPAMyViewController () <UIAlertViewDelegate, UITextFieldDelegate>

    @property (strong, nonatomic) NSString *errorFromRequest;
    @property (strong, nonatomic) UIAlertView *passwordAlertView;

    - (void)processRequest:(UITextField *)passwordTextField;

    @end

    @implementation MPAMyViewController

    - (IBAction)showPasswordConfirmationAlert:(id)sender {
        self.passwordAlertView = [[UIAlertView alloc] initWithTitle:MPAPasswordConfirmationAlertTitle
                                                            message:self.errorFromRequest
                                                           delegate:self
                                                  cancelButtonTitle:MPAPasswordConfirmationAlertCancelButtonTitle
                                                  otherButtonTitles:MPAPasswordConfirmationAlertOkButtonTitle, nil];

        self.passwordAlertView.alertViewStyle = UIAlertViewStyleSecureTextInput;

        UITextField *passwordTextField = [self.passwordAlertView textFieldAtIndex:0];
        [passwordTextField becomeFirstResponder];
        passwordTextField.placeholder = MPAPasswordConfirmationTextFieldPlaceholder;
        passwordTextField.delegate = self;

        [self.passwordAlertView show];
    }


    #pragma mark - UIAlertViewDelegate

    - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
        NSString *title = [alertView buttonTitleAtIndex:buttonIndex];

        if([title isEqualToString:MPAPasswordConfirmationAlertOkButtonTitle]) {
            UITextField *passwordTextField = [alertView textFieldAtIndex:0];
            [self processRequest:passwordTextField];
        }
    }


    #pragma mark - TextField Delegate

    - (BOOL)textFieldShouldReturn:(UITextField *)textField {
        [self.passwordAlertView dismissWithClickedButtonIndex:self.passwordAlertView.firstOtherButtonIndex animated:YES];

        return YES;
    }

    - (void)processRequest:(UITextField *)passwordTextField {
        NSString *password = passwordTextField.text;

        // CODE: send request
    }

    @end
4

4 回答 4

0

使用 alertView ,使用委托并使用 UIAlertView 的任何 Delegate 方法。您尚未将委托设置为当前视图控制器,因此请尝试以下代码

使用

 UIAlertView * alertView  = UIAlertView()
  viewDidLoad(){

 alertView.delegate = self
}

使用上面的代码,您委托的方法将被调用。

于 2015-03-03T11:48:52.817 回答
0

我遇到了类似的问题,UIAlertView Delegatemethod 没有被调用。我发现 UIAlertView 在 iOS 8 中已弃用。您应该使用 UIAlertController。

于 2015-03-03T11:08:11.117 回答
0

使用 UIAlertController 使其与 iOS 8 兼容

于 2015-03-03T11:11:18.163 回答
0

试试下面的代码片段。

#pragma mark - TextField Delegate

- (BOOL)textFieldShouldReturn:(UITextField *)textField {  
    return NO;
}

注意:如果您将上述委托用于其他文本字段,请根据您的要求编写条件代码。

于 2015-08-02T12:41:41.470 回答