UIAlertController的用法示例

来源:互联网 发布:visual studio python 编辑:程序博客网 时间:2024/05/18 21:39

在iOS8中 我们熟悉的UIAlertView已经不被苹果提倡了 取而代之的是UIAlertController  关于详细的解释说明上一篇转载的文章里  已经很详细的介绍了  这篇文章 主要来自己敲一下代码 顺便看一下实现的效果有什么不同

 

//    1.Alert

    UIAlertController *controller = [UIAlertControlleralertControllerWithTitle:nilmessage:@"确定要注销吗"preferredStyle:UIAlertControllerStyleAlert];

    UIAlertAction *okAction = [UIAlertActionactionWithTitle:@"确定"style:UIAlertActionStyleDestructivehandler:^(UIAlertAction *_Nonnull action) {

        [self.navigationControllerpopToRootViewControllerAnimated:YES];

    }];

    UIAlertAction *cancleAction = [UIAlertActionactionWithTitle:@"取消"style:UIAlertActionStyleCancelhandler:nil];

    [controller addAction:okAction];

    [controller addAction:cancleAction];

    [selfpresentViewController:controlleranimated:YEScompletion:nil];

   实现的效果是这样的:



//    2.ActionSheet

    UIAlertController *controller = [UIAlertControlleralertControllerWithTitle:@"Tips"message:@"确定要注销吗"preferredStyle:UIAlertControllerStyleActionSheet];

    UIAlertAction *okAction = [UIAlertActionactionWithTitle:@"确定"style:UIAlertActionStyleDestructivehandler:^(UIAlertAction *_Nonnull action) {

        [self.navigationControllerpopToRootViewControllerAnimated:YES];

    }];

    UIAlertAction *cancleAction = [UIAlertActionactionWithTitle:@"取消"style:UIAlertActionStyleCancelhandler:nil];

    [controller addAction:okAction];

    [controller addAction:cancleAction];

    [selfpresentViewController:controlleranimated:YEScompletion:nil];


实现效果是这样的:


 //3.textfield

   #pragma 注意 如果想加入textfield 类型只能是StyleAlert

    UIAlertController *controller = [UIAlertControlleralertControllerWithTitle:@"Tips"message:@"请输入用户名密码"preferredStyle:UIAlertControllerStyleAlert];

    [controller addTextFieldWithConfigurationHandler:^(UITextField *_Nonnull textField) {

        textField.placeholder  = @"用户名";

    }];

    [controller addTextFieldWithConfigurationHandler:^(UITextField *_Nonnull textField) {

        textField.placeholder = @"密码";

        textField.secureTextEntry = YES;

    }];

    UIAlertAction *okAction = [UIAlertActionactionWithTitle:@"确定"style:UIAlertActionStyleDestructivehandler:^(UIAlertAction *_Nonnull action) {

        [self.navigationControllerpopToRootViewControllerAnimated:YES];

    }];

    UIAlertAction *cancelAction = [UIAlertActionactionWithTitle:@"取消"style:UIAlertActionStyleCancelhandler:nil];

    

    

    [controller addAction:okAction];

    [controller addAction:cancelAction];

    [selfpresentViewController:controlleranimated:YEScompletion:nil];

  实现效果是这样的:


最后加上对textfield用户名长度判断处理的代码

- (IBAction)logout:(UIBarButtonItem *)sender {

   #pragma 注意 如果想加入textfield 类型只能是StyleAlert

    controller = [UIAlertControlleralertControllerWithTitle:@"Tips"message:@"请输入用户名密码"preferredStyle:UIAlertControllerStyleAlert];

    

    /*增加一个监听*/

    [controlleraddTextFieldWithConfigurationHandler:^(UITextField *_Nonnull textField) {

        textField.placeholder  = @"用户名(长度至少为3)";

        [[NSNotificationCenter defaultCenter]addObserver:selfselector:@selector(usernameChanged)name:UITextFieldTextDidChangeNotificationobject:textField];

    }];

    [controlleraddTextFieldWithConfigurationHandler:^(UITextField *_Nonnull textField) {

        textField.placeholder = @"密码";

        textField.secureTextEntry = YES;

    }];

        /*当确定按钮按下时 读取*/

    okAction = [UIAlertActionactionWithTitle:@"确定"style:UIAlertActionStyleDestructivehandler:^(UIAlertAction *_Nonnull action) {

    [[NSNotificationCenterdefaultCenter]removeObserver:self];

    }];

    UIAlertAction *cancelAction = [UIAlertActionactionWithTitle:@"取消"style:UIAlertActionStyleCancelhandler:^(UIAlertAction *_Nonnull action) {

        [self.navigationControllerpopToRootViewControllerAnimated:YES];

    }];

    [controller addAction:okAction];

    [controller addAction:cancelAction];

    okAction.enabled =NO;

    [selfpresentViewController:controlleranimated:YEScompletion:nil];

}

- (void)usernameChanged{

    okAction.enabled =controller.textFields.firstObject.text.length >=3;

}


实现效果是这样的:

长度没有3位:


如果长度达到了3位 显示效果是这样的

0 0
原创粉丝点击