NS_CLASS_DEPRECATED_IOS(2_0, 9_0, "UIAlertView is deprecated. Use UIAlertController with a preferredStyle of UIAlertControllerStyleAlert instead")
@interface UIAlertView : UIView

UIAlertView在iOS9中被彻底废弃,官方说用UIAlertController替代,研究了一下

// UIAlertController从iOS8开始加入的:
NS_CLASS_AVAILABLE_IOS(8_0) @interface UIAlertController : UIViewController


// UIAlertController非常强大,建议多查看官方文档!!
 
 - (void)alertTest1 {
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"提示" message:@"www.dllhook.com\n优雅人生" preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        NSLog(@"确定按钮被点击");
    }];
    UIAlertAction *canselAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        NSLog(@"取消按钮被点击");
    }];

    [alertController addAction:okAction];
    [alertController addAction:canselAction];
    
    [self presentViewController:alertController animated:YES completion:nil];
}
 
 
 - (void)alertTest2 {
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"请输入金额" message:@"金额不能小于10" preferredStyle:UIAlertControllerStyleAlert];
    [alertController addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
        textField.keyboardType = UIKeyboardTypeNumberPad;
        textField.placeholder = @"输入所需金额";
    }];
    UIAlertAction *canselAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
    UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        UITextField *textField = [[alertController textFields] firstObject];
        if (textField.text.length == 0) {
            // 这里加入自己的逻辑
            NSLog(@"金额不能为0");
        }else if([textField.text integerValue] <= 10){
            NSLog(@"金额太小");
        }else{
            NSLog(@"您输入的金额为:%@", textField.text);
        }
    }];
    [alertController addAction:canselAction];
    [alertController addAction:okAction];
    [self presentViewController:alertController animated:YES completion:nil];
}


你可能感兴趣的文章

评论区

发表评论

必填

选填

选填

必填

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。