IOS学习之旅之5 对话框的展现

背景:这一章让我们来介绍一下ios对话框的展现形式,由简至深的学习

内容:1.普通对话框

           2.带有button的对话框

           3.带有输入框的对话框

           4.带有下拉列表的对话框

关于对话框的内容比较简单,所以废话少说,直接上代码和效果图

1.普通对话框

-(IBAction)buttonPressed2:(id)sender//普通的样式
{
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"标题" message:@"UIAlert默认样式" preferredStyle:UIAlertControllerStyleAlert];

    [self presentViewController:alert animated:true completion:nil];
    
}



2.带有按键的对话框

-(IBAction)buttonPressed1:(id)sender//button的样式
{
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"标题" message:@"UIAlert默认样式" preferredStyle:UIAlertControllerStyleAlert];
    
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"cancle" style:UIAlertActionStyleCancel handler:nil];
    UIAlertAction * confirmAction = [UIAlertAction actionWithTitle:@"confirm" style:UIAlertActionStyleDefault handler:nil];
    [alert addAction:cancelAction];
    [alert addAction:confirmAction];
    [self presentViewController:alert animated:true completion:nil];
    
}


带有多个button的对话框

-(IBAction)buttonPressed2:(id)sender//添加button的样式
{
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"标题" message:@"UIAlert默认样式" preferredStyle:UIAlertControllerStyleAlert];
    
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"reset" style:UIAlertActionStyleDestructive handler:nil];
    UIAlertAction * confirmAction = [UIAlertAction actionWithTitle:@"cancel" style:UIAlertActionStyleDefault handler:nil];
        UIAlertAction * confirm1Action = [UIAlertAction actionWithTitle:@"cancel" style:UIAlertActionStyleDefault handler:nil];
            UIAlertAction * confirm2Action = [UIAlertAction actionWithTitle:@"cancel" style:UIAlertActionStyleDefault handler:nil];
    [alert addAction:cancelAction];
    [alert addAction:confirmAction];
    [alert addAction:confirm1Action];
    [alert addAction:confirm2Action];

    [self presentViewController:alert animated:true completion:nil];
    
}

3.带有输入框的button

-(IBAction)buttonPressed4:(id)sender//添加输入框的样式
{
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"标题" message:@"UIAlert默认样式" preferredStyle:UIAlertControllerStyleAlert];
    
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"cancel" style:UIAlertActionStyleCancel handler:nil];
    UIAlertAction * confirmAction = [UIAlertAction actionWithTitle:@"confirm" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action){
        UITextField *login = alert.textFields.firstObject;
        UITextField *password = alert.textFields.lastObject;
        NSLog(@"LOGIN=%@",login);
        NSLog(@"password=%@",password);
        [[NSNotificationCenter defaultCenter]removeObserver:self name:UITextFieldTextDidChangeNotification object:nil];
        

    }];
    confirmAction.enabled = false;
    [alert addAction:cancelAction];
    [alert addAction:confirmAction];
    
    
    [alert addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
        textField.placeholder = @"登录";
        [[NSNotificationCenter defaultCenter ] addObserver:self selector:@selector(alertTextFieldDidChange:) name:UITextFieldTextDidChangeNotification object:textField];

         
         }];
    [alert addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
        textField.placeholder = @"密码";
        textField.secureTextEntry = YES;
        

    }];
    
    
    
    [self presentViewController:alert animated:true completion:nil];
    
}

- (void)alertTextFieldDidChange:(NSNotification *)notification{
         UIAlertController *alertController = (UIAlertController *)self.presentedViewController;
         if (alertController) {
             UITextField *login = alertController.textFields.firstObject;
             UIAlertAction *okAction = alertController.actions.lastObject;
             okAction.enabled = login.text.length > 2;
         }
}

4.下拉对话框

-(IBAction)buttonPressed2:(id)sender //下拉对话框的使用
{
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"下拉标题" message:@"下拉信息"
                                                preferredStyle:UIAlertControllerStyleActionSheet];
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
    UIAlertAction *confirmAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:nil];
        UIAlertAction *deleteAction = [UIAlertAction actionWithTitle:@"删除" style:UIAlertActionStyleDestructive handler:nil];
    [alert addAction:cancelAction];
    [alert addAction:confirmAction];
    [alert addAction:deleteAction];
    [self presentViewController:alert animated:true completion:nil];

}



参考文章:http://www.cocoachina.com/ios/20141126/10320.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值