uikit 中uialert 的使用方法UIAlertController

来源:互联网 发布:c语言中左移 编辑:程序博客网 时间:2024/06/05 23:47

现在ios8以后UIAlertController在功能上替代了uialertviewuiactionsheet

在定义一个alert控件时,定义的UIAlertControllerUIAlertAction要在响应的函数中添加。

下面的例子是:

当 按下 button 按钮时 跳出警告对话框。

1.先用main.storyboard 创建一个button

2.创建 一个响应函数(touchinside)

@IBActionfunc TouchButton(_ sender:UIButton) {

//添加alert的代码

}

 

3.创建alert

let alert =UIAlertController(title:"标题", message:"你是否要关闭", preferredStyle:UIAlertControllerStyle.alert)

        let actionOk=UIAlertAction(title:"确认", style:UIAlertActionStyle.default, handler:nil)

 alert.addAction(actionOk) 

        letactionCancel =UIAlertAction(title:"取消", style:UIAlertActionStyle.cancel, handler:nil)

             alert.addAction(actionCancel)

        self.present(alert, animated:true,completion:nil)

 

//这里的取消 按钮 默认 在 左边。

原创粉丝点击