UIAlertController使用——展示AlertView和ActionSheet

在iOS8之前使用UIAlertView和UIActionSheet来展示警告框和操作表,自iOS8之后统一为通过UIAlertController来实现,它继承与UIViewController

展示AlertView

直接上实例代码

@IBAction func showAlertView(sender:AnyObject) {
        
        //1.实例化UIAlertController
        let alertController = UIAlertController.init(title: "这里填标题", message: "这里填正文内容(可以不填)", preferredStyle: UIAlertControllerStyle.alert)
        
        //2.添加操作按钮(如果你有多个按钮,则重复此步骤)
        let delectAction = UIAlertAction.init(title: "删除", style: UIAlertActionStyle.destructive) { (alert:UIAlertAction!) in
            //这里添加按钮按下后处理的动作
        }
        alertController.addAction(delectAction)
        
            //重复步骤2
        let confirmAction = UIAlertAction.init(title: "取消", style: UIAlertActionStyle.default) { (alert:UIAlertAction!) in
            //这里添加按钮按下后处理的动作
        }
        alertController.addAction(confirmAction)
        
        //3.展示警告框
        self.present(alertController, animated: true, completion: nil)
        
    }
运行效果

备忘:

  • 要显示警告框,实例化UIAlertController时,preferredStyle要填写UIAlertControllerStyle.alert
  • 所有按钮的回调在按钮初始化时在block中定义
  • 添加的按钮的类型为UIAlertActionStyle,不同类型外观有所不同
  • 添加按钮的顺序影响展示的顺序。

展示ActionShet

与上面的步骤大同小异,唯一不同的是,需要在UIAlertController实例化时,preferredStyle中填写UIAlertControllerStyle.actionSheet

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

相关阅读更多精彩内容

友情链接更多精彩内容