UIAlertController

UIAlertController
iOS2.0之后,使用UIAlertView来设置提示框,使用initWithTitle:message: delegate:self cancelButtonTitle:otherButtonTitles:初始化alertView,然后使用-show显示alert。如果想获取button则需要使用delegate方法。(UIActionSheet类似)
在IOS8之后,UIAlertController替代了UIActionSheet和UIAlertView。是UIViewController的子类,它使用工厂方法,利用preferredStyle属性,暂时提供alert以及actionSheet(实际使用时,使用alertControllerWithTitle:message:preferredStyle中preferredStyle即可制定提示框的类型)。
注意,
(1) 这个class不能通过继承的方式来自定义。
(2)UIAlertController必须等到View Controller加载后才能显示,如果将UIAlertController类写在View Controller中没有效果。

1、下面是常用的属性和方法

//在actionWithTitle:Style:handler中的style参数,设置按钮的style
typedef NS_ENUM(NSInteger, UIAlertActionStyle) {
    UIAlertActionStyleDefault = 0,
    UIAlertActionStyleCancel,//取消
    UIAlertActionStyleDestructive//销毁,红色的字体
} NS_ENUM_AVAILABLE_IOS(8_0);

//alertControllerWithTitle:message:preferredStyle:初始化UIAlertController时,perferredStyle的参数,表示信息框是ActionSheet还是Alert。
typedef NS_ENUM(NSInteger, UIAlertControllerStyle) {
    UIAlertControllerStyleActionSheet = 0,
    UIAlertControllerStyleAlert
} NS_ENUM_AVAILABLE_IOS(8_0);

@interface UIAlertAction : NSObject <NSCopying>

+(instancetype)actionWithTitle:(nullable NSString *)title style:(UIAlertActionStyle)style handler:(void (^ __nullable)(UIAlertAction *action))handler;

@property (nullable, nonatomic, readonly) NSString *title;
@property (nonatomic, readonly) UIAlertActionStyle style;
@property (nonatomic, getter=isEnabled) BOOL enabled;

@end

@interface UIAlertController : UIViewController

+(instancetype)alertControllerWithTitle:(nullable NSString *)title message:(nullable NSString *)message preferredStyle:(UIAlertControllerStyle)preferredStyle;

//添加向controller中添加action
- (void)addAction:(UIAlertAction *)action;
@property (nonatomic, readonly) NSArray<UIAlertAction *> *actions;

@property (nonatomic, strong, nullable) UIAlertAction *preferredAction NS_AVAILABLE_IOS(9_0);

//添加文本框
- (void)addTextFieldWithConfigurationHandler:(void (^ __nullable)(UITextField *textField))configurationHandler;
@property (nullable, nonatomic, readonly) NSArray<UITextField *> *textFields;//获取所有的文本框

@property (nullable, nonatomic, copy) NSString *title;
@property (nullable, nonatomic, copy) NSString *message;

@property (nonatomic, readonly) UIAlertControllerStyle preferredStyle;

@end

2、例子:使用警示框输入账户、密码,实现登录。

-(void)AlertAchieveLogin{

    //1、初始化UIAlertController
    /*
     +alertControllerWithTitle:message:preferredStyle:声明
     title:UIAlertController的标题
     message:title下面的解释文字
     preferredStyle:AlertController是Alert还是ActionSheet
     */
    UIAlertController *controller = [UIAlertController alertControllerWithTitle:@"登录" message:@"请输入姓名和密码" preferredStyle:UIAlertControllerStyleAlert];

    //2、UIAlertAction设置按钮
    //取消按钮,alert信息框消失
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {

        [self dismissViewControllerAnimated:YES completion:nil];

    }];

    //登录按钮
    /*
     +actionWithTitle:style:handler:
     title:action的标题
     style:alertAction的风格,UIAlertActionStyleDefault,UIAlertActionStyleCancel,UIAlertActionStyleDestructive
     handler:处理用户按下该按钮后要做的操作
     */
    UIAlertAction *loginAction = [UIAlertAction actionWithTitle:@"登录" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {

        //点击登录按钮后,获取账户和密码
        NSString *account = [controller.textFields objectAtIndex:0].text;
        NSString *pwd = [controller.textFields objectAtIndex:1].text;

        NSLog(@"账户是:%@,密码:%@",account,pwd);

    }];

    //3、向UIAlertController添加按钮
    [controller addAction:cancelAction];
    [controller addAction:loginAction];

    //4、设置文本框
    [controller addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {

        //在文本框中,显示account
        textField.placeholder = @"account";
        textField.layer.masksToBounds = YES;
        textField.layer.cornerRadius  = 10;
        textField.backgroundColor = [UIColor lightGrayColor];

    }];

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

        //在文本框中,显示password
        textField.placeholder = @"password";
        textField.layer.masksToBounds = YES;
        textField.layer.cornerRadius  = 10;


    }];



    //5、展示信息框
    //使用presentViewController:animated:completion:呈现alertController
    [self presentViewController:controller animated:YES completion:nil];


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

程序员的修养

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值