美文网首页
iOS9提示框UIAlertController

iOS9提示框UIAlertController

作者: Nedoloroso | 来源:发表于2015-10-29 22:56 被阅读2193次
  • iOS中的提示框分为在中间显示


    中部.png
  • 和在下方显示两种


    下方.png

iOS8之前

  • 中部的提示框使用的是UIAlertView
  • 底部的提示框使用的是UIActionSheet;

iOS8开始及iOS9之后

  • 使用的是UIAlertController
  • 相当于UIAlertController == UIAlertView + UIActionSheet
  • 首先进行初始化
 UIAlertController *alert = [UIAlertController alertControllerWithTitle:<#(nullable NSString *)#> message:<#(nullable NSString *)#> preferredStyle:<#(UIAlertControllerStyle)#>]
* Title和message是看自己的需求传值,要实现上图中的效果只需要传nil即可,preferredStyle是设置提示框的类型,有两种可以选择
    // 底部提示框
    UIAlertControllerStyleActionSheet
    // 中部提示框
    UIAlertControllerStyleAlert
  • 添加按钮
    [alert addAction:[UIAlertAction actionWithTitle:@"收藏" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
    }]];
    [alert addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
    }]];
* 这里的style也有三个可以选择,根据自己需求选择即可
    UIAlertActionStyleDefault,  //默认
    UIAlertActionStyleCancel,  //取消
    UIAlertActionStyleDestructive  //警告
  • 最后就是在window上modal出提示框即可
    [self.window.rootViewController presentViewController:alert animated:YES completion:nil];

相关文章

网友评论

      本文标题:iOS9提示框UIAlertController

      本文链接:https://www.haomeiwen.com/subject/rfcfhttx.html