在 DidFinishLaunchingWithOptions 中带有 TextField 的 AlertView

AlertView with TextField in DidFinishLaunchingWithOptions

我有这样的场景,在第一次安装应用程序时,用户应该查看 UIAlertViewControllerUITextField,用户应该输入电子邮件 ID,如果有效则关闭 UIAlertViewController , else 不忽略 UIAlertViewController 需要提示其他 UIAlertViewController 说 "Enter Valid Email ID", 如何实现这个?

请将此写在AppDelegate.m中。

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.



    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Title"
                                                    message:@"Message"
                                                   delegate:self
                                          cancelButtonTitle:@"Done"
                                          otherButtonTitles:nil];
    alert.tag = 1000;
    alert.alertViewStyle = UIAlertViewStylePlainTextInput;
    [alert show];

    return YES;
}
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    if (alertView.tag == 1000) {
        NSLog(@"%@", [alertView textFieldAtIndex:0].text);
        NSString *str = [alertView textFieldAtIndex:0].text;
        if ([str isEqualToString:@"your string"]) {

            //do what ever you want.
        }else{
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Wrong Email"
                                                            message:@"Please try again"
                                                           delegate:self
                                                  cancelButtonTitle:@"OK"
                                                  otherButtonTitles:nil];
            alert.tag = 1001;
            [alert show];

        }
    }

}

AppDelegate.h 中的 UIActionSheet 和 TextField 的委托添加为

@interface AppDelegate : UIResponder <UIApplicationDelegate, UIActionSheetDelegate>

在 AppDelegate 中添加 UIActionSheet 和 TextField 的委托为 首先检查用户打开应用程序的时间。为此,您可以在 AppDelegate's 中使用此代码 applicationDidFinishLaunching方法:

if (![[NSUserDefaults standardUserDefaults] boolForKey:@"HasLaunchedOnce"])
    {
        [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"HasLaunchedOnce"];
        [[NSUserDefaults standardUserDefaults] synchronize];
 //Now set up an alertView with textField:
UIAlertView *alertView1 = [[UIAlertView alloc] initWithTitle:@"ALERT!!!" message:@“Enter Email ID” delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Ok", nil];
            alertView1.alertViewStyle = UIAlertViewStyleSecureTextInput;
            self.passwordField = [alertView1 textFieldAtIndex:0];
            [alertView1 setTag:10003];
            self.passwordField.keyboardType=UIKeyboardTypeAlphabet;
            [alertView1 show];
}
}

现在将 alertView 的委托方法设置为:

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
    if(alertView.tag == 10003) {

        if (buttonIndex==1) {
            [self.passwordField resignFirstResponder];
            NSString *emailRegex = @"[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}";
      NSPredicate *emailTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", emailRegex];
    if ([emailTest evaluateWithObject:email])
     {
     //Do nothing

     } 
            else {
                //Show alert View for failed case
                UIAlertView *al = [[UIAlertView alloc] initWithTitle:@"Alert" message:@"Please Enter Valid Email" delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil];
                [al show];
                al.tag = 10004;
            }
        }

为您的 didFinishLaunchingWithOptions 添加一些逻辑。

这里的场景是,它会首先显示提示,说输入您的电子邮件。 当您输入电子邮件并单击确定时,它会检查电子邮件是否有效。 如果它不是有效的,则显示另一个显示您的电子邮件无效的警报。 同时,您的第一个警报将在那时被解除。当您再次按下 AlertError 的 OK 时。它将再次显示您的 textField 电子邮件的 AlertController。

-(void)Alert{
   
    UIAlertController * alertController = [UIAlertController alertControllerWithTitle: @"Your Email" message: @"Enter Your Email"preferredStyle:UIAlertControllerStyleAlert];
    
    [alertController addTextFieldWithConfigurationHandler:^(UITextField *textField) {
        textField.placeholder = @"E-mail";
        textField.textColor = [UIColor blueColor];
        textField.clearButtonMode = UITextFieldViewModeWhileEditing;
        textField.borderStyle = UITextBorderStyleRoundedRect;
    }];
    
    [alertController addAction:[UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
        NSArray * textfields = alertController.textFields;
        UITextField * Email = textfields[0];
        
        if ([self validateEmailWithString:Email.text]) {
        }else{
            UIAlertController *alertError = [UIAlertController  alertControllerWithTitle:@"Error"  message:nil  preferredStyle:UIAlertControllerStyleAlert];
            
            [alertError addAction:[UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action)
                {
            [self dismissViewControllerAnimated:YES completion:nil];
                    
            [self presentViewController:alertController animated:YES completion:nil];

            }]];
            
            [self presentViewController:alertError animated:YES completion:nil];
        }
    }]];
    [self presentViewController:alertController animated:YES completion:nil];
    
}
- (BOOL)validateEmailWithString:(NSString*)checkString
{
    BOOL stricterFilter = NO;
    NSString *stricterFilterString = @"[A-Z0-9a-z\._%+-]+@([A-Za-z0-9-]+\.)+[A-Za-z]{2,4}";
    NSString *laxString = @".+@([A-Za-z0-9-]+\.)+[A-Za-z]{2}[A-Za-z]*";
    NSString *emailRegex = stricterFilter ? stricterFilterString : laxString;
    NSPredicate *emailTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", emailRegex];
    return [emailTest evaluateWithObject:checkString];
}

这将非常适合您。

现在如果有错误再次出现邮件提醒。