提示框应用UIAlertController

准备工作(以下代码不用写)

typedef NS_ENUM(NSInteger, UIAlertControllerStyle) {
   UIAlertControllerStyleActionSheet = 0,// 从底部弹出
   UIAlertControllerStyleAlert// 从中心弹出
} NS_ENUM_AVAILABLE_IOS(8_0);

typedef NS_ENUM(NSInteger, UIAlertActionStyle) {
   UIAlertActionStyleDefault = 0,
   UIAlertActionStyleCancel,
   UIAlertActionStyleDestructive
} NS_ENUM_AVAILABLE_IOS(8_0);

上干货

UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"文本对话框" message:@"登录和密码对话框" preferredStyle:UIAlertControllerStyleAlert];

UIAlertAction *OKAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
    // 停止监听文本改变通知
    [[NSNotificationCenter defaultCenter]removeObserver:self name:UITextFieldTextDidChangeNotification object:alertController.textFields.lastObject];
    // 打印第一个文本框的内容
    NSLog(@"%@",alertController.textFields.firstObject.text);
    
}];
[alertController addAction:OKAction];

UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
[alertController addAction:cancelAction];

 UIAlertAction *destructiveAction = [UIAlertAction actionWithTitle:@"警告" style:UIAlertActionStyleDestructive handler:nil];
[alertController addAction:destructiveAction];

[alertController addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
    textField.backgroundColor = [UIColor redColor];
    textField.placeholder = @"请输用户名";   
}];

[alertController addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
    textField.backgroundColor = [UIColor redColor];
    textField.placeholder = @"请输入密码";
    textField.secureTextEntry = YES;// 以圆点格式显示
    // 设置监听
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(handleTextDidChangeNotification:) name:UITextFieldTextDidChangeNotification object:textField];
    
}];

// 将OK按钮禁用
OKAction.enabled = NO;
self.alertAction = OKAction;//存储OK按钮

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

从底部弹出提示框

UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"OK/取消" message:@"请你登陆!" preferredStyle:UIAlertControllerStyleActionSheet];

UIAlertAction *OKaction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:nil];

UIAlertAction *cancleAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];

UIAlertAction *destructiveAction = [UIAlertAction actionWithTitle:@"警告" style:UIAlertActionStyleDestructive handler:nil];

[alertController addAction:OKaction];
[alertController addAction:cancleAction];
[alertController addAction:destructiveAction];

[self presentViewController:alertController animated:YES completion:nil];
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 173,315评论 25 708
  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 12,241评论 4 61
  • 老实说李安的电影真的很细腻。老套的剧情,甚至有点过于简单,都被拍出花了。 比利和他的兄弟们站在舞台上的时候,眼前是...
    南沧阅读 422评论 0 0
  • 周六的夜晚 暖光的台灯 许嵩的老歌 趴在枕头上看书 人人都是产品经理 亲爱的人在旁边摆弄鱼钩线 偶尔一句交谈 真爱...
    lulululun阅读 148评论 0 2