在DidFinishLaunchingWithOptions中使用TextField的AlertView

时间:2016-11-24 05:26:18

标签: ios objective-c uialertcontroller

我有一个方案,例如,在第一次安装应用时,用户应该使用<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Test</title> <link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css"> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css"> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script> <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script> <script src="Sortable.min.js"></script> <style type="text/css"> #tab-list .close{ margin-left: 7px; } </style> <script type="text/javascript"> $(document).ready(function () { var tabID = 1; $('#btn-add-tab').click(function () { tabID++; $('#tab-list').append($('<li><a href="#tab' + tabID + '" role="tab" data-toggle="tab">Tab ' + tabID + '<button class="close" type="button" title="Remove this page">×</button></a></li>')); $('#tab-content').append($('<div class="tab-pane fade" id="tab' + tabID + '">Tab '+ tabID +' content </div>')); }); $('#btn-show-tabs').click(function () { alert('Tabs'); }); $('#tab-list').on('click','.close',function(){ var tabID = $(this).parents('a').attr('href'); $(this).parents('li').remove(); $(tabID).remove(); //display first tab var tabFirst = $('#tab-list a:first'); tabFirst.tab('show'); }); var list = document.getElementById("tab-list"); new Sortable(list); }); </script> </head> <body> <!-- Container Start --> <div class="container-fluid"> <!-- Main Content Area Start --> <div class="main-container"> <!--- ---> <div class="container"> <div class="row"> <div class="col-md-12"> <p> <button id="btn-add-tab" type="button" class="btn btn-primary">Add Tab</button> &nbsp <button id="btn-show-tabs" type="button" class="btn btn-primary pull-right">Show Tabs</button><br> </p> <!-- Nav tabs --> <ul id="tab-list" class="nav nav-tabs" role="tablist"> <li class="active"><a href="#tab1" role="tab" data-toggle="tab">Tab 1</a></li> </ul> <!-- Tab panes --> <div id="tab-content" class="tab-content"> <div class="tab-pane fade in active" id="tab1"> Tab 1 content </div> </div> </div> </div> <!--- ---> </div> <!-- Main Content Area End --> <div class="clearfix"></div> </div> <!-- Container End --> </body> </html> 查看UIAlertViewController,用户应该输入电子邮件ID,如果有效则忽略UITextField,否则在不解释UIAlertViewController需要提示其他UIAlertViewController Saying&#34;输入有效电子邮件ID&#34;,如何实现这一目标的情况下?

3 个答案:

答案 0 :(得分:1)

请在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];

        }
    }

}

答案 1 :(得分:1)

为didFinishLaunchingWithOptions添加一些逻辑。

此处的情景是,它将首先显示警报说输入您的电子邮件。 当您输入电子邮件并单击确定时,它会检查电子邮件是否有效。 如果不是有效,则显示您的电子邮件的另一个警报无效。 同时你的第一个警报将被解雇。当你再次按下AlertError的OK。它将再次显示textField Email的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];
}

这对你来说很完美。

enter image description here

enter image description here

现在,如果再次出现错误,则会显示电子邮件提醒。

答案 2 :(得分:0)

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

@interface AppDelegate : UIResponder <UIApplicationDelegate, UIActionSheetDelegate>

在AppDelegate中添加UIActionSheet和TextField的委托 首先检查用户正在打开应用程序的idk。为此,您可以在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;
            }
        }