iOS开发UIAlertController常用属性方法

UIAlertController是iOS8推出的新概念,取代了之前的 UIAlertView和UIActionSheet(虽然现在仍可以使用,但是会有警告)。所以还是建议大家以后还是使用UIAlertController吧(本人看见警告就有种冲动?),下面有些代码有点重复,但是能过为了更清楚的看到效果

//

//  ViewController.m

//  UIAlertControllerAll

#import "ViewController.h"

 

@interface ViewController ()<UIAlertViewDelegate>

 

@end

 

@implementation ViewController

 

- (void)viewDidLoad {

    [super viewDidLoad];

    self.view.backgroundColor = [UIColor yellowColor];

    

    //点击弹出alert的按钮

    UIButton *First_Button = [UIButton buttonWithType:UIButtonTypeCustom];

    First_Button.frame = CGRectMake(30, 50, 100, 40);

    [First_Button setBackgroundColor:[UIColor brownColor]];

    [First_Button setTitle:@"标准Alert" forState:UIControlStateNormal];

    [First_Button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];

    First_Button.tag = 111;

    [self.view addSubview:First_Button];

    

    UIButton *Second_Button = [UIButton buttonWithType:UIButtonTypeCustom];

    Second_Button.frame = CGRectMake([UIScreen mainScreen].bounds.size.width - 130, 50, 100, 40);

    [Second_Button setBackgroundColor:[UIColor brownColor]];

    [Second_Button setTitle:@"标准Sheet" forState:UIControlStateNormal];

    [Second_Button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];

    Second_Button.tag = 222;

    [self.view addSubview:Second_Button];

    

    UIButton *Third_Button = [UIButton buttonWithType:UIButtonTypeCustom];

    Third_Button.frame = CGRectMake([UIScreen mainScreen].bounds.size.width / 2.0 - 50, 140, 100, 40);

    [Third_Button setBackgroundColor:[UIColor brownColor]];

    [Third_Button setTitle:@"多按钮操作" forState:UIControlStateNormal];

    [Third_Button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];

    Third_Button.tag = 333;

    [self.view addSubview:Third_Button];

    

    

}

- (void)buttonAction:(UIButton *)button

{

    if (button.tag == 111) {

        NSLog(@"Alert");

        //UIAlertController风格:UIAlertControllerStyleAlert

        UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"没有标题的标题"

                                                                                 message:@"学无止境,漫漫长路"

                                                                          preferredStyle:UIAlertControllerStyleAlert ];

        

        //添加取消到UIAlertController中

        UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {

            NSLog(@"Alert 取消");

        }];

        [alertController addAction:cancelAction];

        

        //添加确定到UIAlertController中

        UIAlertAction *OKAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {

            NSLog(@"Alert 确定");

        }];

        [alertController addAction:OKAction];

        

        __weak __typeof(self) weakSelf = self;

        

        //添加输入框

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

            textField.placeholder = @"美女芳名啊?";

            [[NSNotificationCenter defaultCenter]addObserver:weakSelf selector:@selector(TextFieldDidChanged:) name:UITextFieldTextDidChangeNotification object:nil];

            

            //UIKIT_EXTERN NSNotificationName const UITextFieldTextDidBeginEditingNotification;

            //UIKIT_EXTERN NSNotificationName const UITextFieldTextDidEndEditingNotification;

            //UIKIT_EXTERN NSNotificationName const UITextFieldTextDidChangeNotification;

            

        }];

        // 添加密码输入框

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

            textField.placeholder = @"请输入密码";

            //这里还可以进行多种设置

            [textField setSecureTextEntry:YES];//设置成隐号形式

        }];

        

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

            textField.placeholder = @"不行啊";

            

        }];

        

        [self presentViewController:alertController animated:YES completion:nil];

        

    }else if(button.tag == 222){

        NSLog(@"Sheet");

        UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"标准的Action Sheet样式"

                                                                                 message:nil

                                                                          preferredStyle:UIAlertControllerStyleActionSheet];

        //取消:style:UIAlertActionStyleCancel

        UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {

            NSLog(@"Sheet 取消");

        }];

        [alertController addAction:cancelAction];

        //了解更多:style:UIAlertActionStyleDestructive

        UIAlertAction *moreAction = [UIAlertAction actionWithTitle:@"了解更多" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {

            NSLog(@"Sheet 了解更多");

        }];

        [alertController addAction:moreAction];

        //原来如此:style:UIAlertActionStyleDefault

        UIAlertAction *OKAction = [UIAlertAction actionWithTitle:@"原来如此" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {

            NSLog(@"Sheet 原来如此");

        }];

        [alertController addAction:OKAction];

        

        [self presentViewController:alertController animated:YES completion:nil];

        

    }else if (button.tag == 333){

        UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"你猜我选哪个呢?"

                                                                                 message:@"美女就是多"

                                                                          preferredStyle:UIAlertControllerStyleActionSheet ];

        

        UIAlertAction *home1Action = [UIAlertAction actionWithTitle:@"腿长" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {

            NSLog(@"多按钮 腿长");

        }];

        [alertController addAction:home1Action];

        UIAlertAction *home2Action = [UIAlertAction actionWithTitle:@"漂亮" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {

            NSLog(@"多按钮 漂亮");

        }];

        [alertController addAction:home2Action];

        UIAlertAction *home3Action = [UIAlertAction actionWithTitle:@"女强人" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {

            NSLog(@"多按钮 女强人");

        }];

        

        [alertController addAction:home3Action];

        UIAlertAction *home4Action = [UIAlertAction actionWithTitle:@"土豪" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {

            NSLog(@"多按钮 土豪");

        }];

        [alertController addAction:home4Action];

        //取消style:UIAlertActionStyleDefault

        UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"全带走" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {

            NSLog(@"多按钮 全带走");

        }];

        [alertController addAction:cancelAction];

        

        [self presentViewController:alertController animated:YES completion:nil];

        

    }

    /*

     style:UIAlertActionStyleDefault//对按钮应用标准样式

     style:UIAlertActionStyleCancel//对按钮应用取消样式,即取消操作

     style:UIAlertActionStyleDestructive//对按钮应用警示性样式,提示用户这样做可能会删除或者改变某些数据

     */

    

}

 

- (void)TextFieldDidChanged:(NSNotification *)notification {

    UIAlertController *alertController = (UIAlertController *)self.presentedViewController;

    if (alertController) {

        UITextField *First_TextField = alertController.textFields.firstObject;

        UIAlertAction *action  = alertController.actions.lastObject;

        action.enabled = First_TextField.text.length > 0;

        NSLog(@"%@", First_TextField.text);

        

        UITextField *Second_TextField = alertController.textFields[1];

        NSLog(@"%@", Second_TextField.text);

        

        UITextField *Third_TextField = alertController.textFields[2];

        NSLog(@"%@", Third_TextField.text);

    }

}

- (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}

 

 

@end

 

转载于:https://www.cnblogs.com/freeleader/p/7411889.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
iOS开发中,有许多常用的库和框架可以帮助开发者提高效率和加速开发过程。以下是一些常用的库和框架: 1. Alamofire:一个简洁的网络请求库,提供了一种优雅的方式来进行网络请求和处理响应。 2. SDWebImage:一个用于异步加载和缓存网络图片的库,可以帮助提高图片加载性能,并且具有内存和磁盘缓存机制。 3. AlamofireImage:一个基于Alamofire的图片加载库,提供了一些便捷的方法来加载网络图片并进行缓存。 4. SwiftyJSON:一个轻量级的、灵活的JSON解析库,可以帮助简化处理JSON数据的过程。 5. SnapKit:一个优雅的、轻量级的Auto Layout框架,使用Swift语言提供了一种简化UI布局代码的方式。 6. Realm:一个移动数据库框架,提供了高效的数据存储和查询功能,并且支持对象关系映射(ORM)。 7. AlamofireObjectMapper:一个将Alamofire与ObjectMapper结合使用的库,可以方便地将JSON数据映射到模型对象中。 8. Kingfisher:一个用于异步加载和缓存网络图片的库,具有高性能和功能丰富的特点。 9. RxSwift:一个用于响应式编程的库,可以简化异步编程和事件处理的复杂性。 10. IQKeyboardManager:一个用于处理键盘弹出和收起的库,可以自动管理键盘,提供了一种简单的方式来避免键盘遮挡输入框的问题。 这只是一小部分常用的库和框架,iOS开发中还有许多其他优秀的工具可供选择,根据具体需求选择合适的库和框架进行开发

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值