iOS-UIAlertController

UIAlertController 取代了 UIAlertView 和 UIActionSheet


屏幕快照 2018-03-21 17.20.20.png

声明:

UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"标题" message:@"这个是UIAlertController的标题" preferredStyle:UIAlertControllerStyleAlert];
/*
typedef NS_ENUM(NSInteger, UIAlertControllerStyle) {
    UIAlertControllerStyleActionSheet = 0,
    UIAlertControllerStyleAlert
}
*/

添加项目:

    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
    UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:nil];
    UIAlertAction *resetAction = [UIAlertAction actionWithTitle:@"重置" style:UIAlertActionStyleDestructive handler:nil];

/*
typedef NS_ENUM(NSInteger, UIAlertActionStyle) {
    UIAlertActionStyleDefault = 0,
    UIAlertActionStyleCancel,
    UIAlertActionStyleDestructive
}
*/
    [alertController addAction:cancelAction];
    [alertController addAction:okAction];
    [alertController addAction:resetAction];

//各个项目的顺序取决于类型,同类型的取决于添加顺序

展示:

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

含对话框的


165455_u3x5_1451688.png
    [alertController addTextFieldWithConfigurationHandler:^(UITextField *textField){
        textField.placeholder = @"登录";
        //添加监听
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(alertTextFieldDidChange:) name:UITextFieldTextDidChangeNotification object:textField];
    }];
    
    [alertController addTextFieldWithConfigurationHandler:^(UITextField *textField) {
        textField.placeholder = @"密码";
        textField.secureTextEntry = YES;
    }];

    UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction *alertAction){
        //移除监听
        [[NSNotificationCenter defaultCenter] removeObserver:self name:UITextFieldTextDidChangeNotification object:nil];
    }];
    okAction.enabled = NO;//设置不可点击

监听方法

- (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;
    }
}

设置颜色

[cancelAction setValue:[UIColor orangeColor] forKey:@"_titleTextColor"];
170129_sZHK_1451688.png
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 在iOS8之前的开发过程中,我们通常使用UIAlertView或者UIActionSheet来提示用户是否进行某项...
    ManoBoo阅读 4,143评论 5 5
  • UIAlertController是iOS8新出的API,它主要是替代以前的UIAlertView和UIActio...
    Fsn_soul阅读 3,579评论 0 0
  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 14,565评论 4 61
  • 公历农历生日重合为一天,19年轮一次。生日是生命的诞生,也是自我的新生。很多人问我,为何为自己取名“大寒...
    大寒翌日Dreamer阅读 1,702评论 0 2
  • 《相约一场雪》 如果有一个梦停留在路口 我会拥抱着幻化的云朵 与你重复一场美丽的邂逅 在细碎如雨的雪里 轻轻地在耳...
    高学海阅读 1,733评论 0 1

友情链接更多精彩内容