UIAlertController进行代替UIAlterView和UIActionSheet

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    UIButton *alterView = [UIButton buttonWithType:(UIButtonTypeCustom)];
    alterView.frame = CGRectMake(100, 100, 100, 100);
    // 给alterView 添加点击方法
    [alterView addTarget:self action:@selector(alterViewButton:) forControlEvents:(UIControlEventTouchUpInside)];
    alterView.backgroundColor = [UIColor blueColor];
    [self.view addSubview:alterView];
    
    UIButton *actionSheet = [UIButton buttonWithType:(UIButtonTypeCustom)];
    actionSheet.frame = CGRectMake(100, 260, 100, 100);
    // 给actionSheet 添加点击方法
    [actionSheet addTarget:self action:@selector(actionSheetButton:) forControlEvents:(UIControlEventTouchUpInside)];
    actionSheet.backgroundColor = [UIColor yellowColor];
    [self.view addSubview:actionSheet];
}

// alterViewButton的点击事件
- (void)alterViewButton:(UIButton *)button
{
 
    
    // 初始化一个一个UIAlertController
    // 参数preferredStyle:是IAlertController的样式
    // UIAlertControllerStyleAlert 创建出来相当于UIAlertView
    // UIAlertControllerStyleActionSheet 创建出来相当于 UIActionSheet
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"身份认证" message:@"请输入密码" preferredStyle:(UIAlertControllerStyleAlert)];
    
    // 创建按钮
    UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"确定" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction *action) {
        [self okButton:alertController.textFields.firstObject.text];
    }];
    // 创建按钮
    // 注意取消按钮只能添加一个
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:(UIAlertActionStyleCancel) handler:^(UIAlertAction *action) {
        // 点击按钮后的方法直接在这里面写
        [self cancelButton];
        
    }];
    
    //    // 创建警告按钮
    //    UIAlertAction *structlAction = [UIAlertAction actionWithTitle:@"警告" style:(UIAlertActionStyleDestructive) handler:^(UIAlertAction *action) {
    //        NSLog(@"注意学习");
    //    }];
    //
    // 添加按钮 将按钮添加到UIAlertController对象上
    [alertController addAction:okAction];
    [alertController addAction:cancelAction];
    //[alertController addAction:structlAction];
    
    // 只有在alert情况下才可以添加文本框
    [alertController addTextFieldWithConfigurationHandler:^(UITextField *textField) {
        textField.placeholder = @"密码";
        textField.secureTextEntry = YES;
        
    }];
    
        // 取出文本
//        UITextField *text = alertController.textFields.firstObject;
//        UIAlertAction *action = alertController.actions.firstObject;
    
    // 将UIAlertController模态出来 相当于UIAlertView show 的方法
    [self presentViewController:alertController animated:YES completion:nil];

    
}
- (void)cancelButton
{
    NSLog(@"取消");
}
- (void)okButton:(NSString *)string
{
    NSLog(@"确定:%@",string);
}
//actionSheet点击事件
- (void)actionSheetButton:(UIButton *)button
{
    //获取手机系统版本
    float version = [[[UIDevice currentDevice] systemVersion] floatValue];
    if (version > 7.99) {
        
        UIAlertController *alertController = [UIAlertController alertControllerWithTitle:nil
                                              
                                                                                 message:nil
                                              
                                                                          preferredStyle:UIAlertControllerStyleActionSheet];
        
        UIAlertAction* cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel
                                       
                                       
                                                             handler:^(UIAlertAction * action) {}];
        
        UIAlertAction* fromPhotoAction = [UIAlertAction actionWithTitle:@"从相册选择" style:UIAlertActionStyleDefault                                                                 handler:^(UIAlertAction * action) {                                                                     UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
            
            imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
            
            imagePicker.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
            
            //        imagePicker.allowsEditing = YES;
            
            imagePicker.allowsEditing = NO;
            
            imagePicker.delegate = self;
            
            [self presentViewController:imagePicker animated:YES completion:nil];
            
        }];
        
        UIAlertAction* fromCameraAction = [UIAlertAction actionWithTitle:@"相机" style:UIAlertActionStyleDefault                                                             handler:^(UIAlertAction * action) {
            
            if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
                
            {
                
                UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
                
                imagePicker.delegate = self;
                
                //            imagePicker.allowsEditing = YES;
                
                imagePicker.allowsEditing = NO;
                
                imagePicker.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
                
                imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
                
                [self presentViewController:imagePicker animated:YES completion:nil];
                
            }
            
        }];
        
        [alertController addAction:cancelAction];
        
        [alertController addAction:fromCameraAction];
        
        [alertController addAction:fromPhotoAction];
        
        [self presentViewController:alertController animated:YES completion:nil];
        
    } else {
        
        UIActionSheet * actionSheet = [[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:@"相机",@"从相册选择", nil];
        
        actionSheet.actionSheetStyle = UIActionSheetStyleBlackOpaque;
        
        actionSheet.delegate = self;
        
        [actionSheet showInView:self.view];
        
    }

}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值