32-Swift之UIAlertController(提示)

一、UIAlertController 提示介绍

提示,在App中许多地方用到提示和用户进行交互。让用户进行自己的选择。

二、UIAlertController---> Alert 的使用和方法

1、 Alert 的初始化

/**
 初始化
 */
NwAlertView = UIAlertController.init()
/**
 弹出提示的类型
 UIAlertControllerStyle
 
 alert :  简单的块型提示。
 actionSheet : 是一种列表型的提示
 */
NwAlertView = UIAlertController.init(title: "选一选", message: "你想要哪个美女?", preferredStyle: .alert)

2、添加交互

/**
 添加交互
 UIAlertActionStyle 交互的类型
 
 cancel  : 取消提示
 default : 默认提示蓝色字体
 destructive : 按钮字体颜色为红色
 */
let DeleteAction = UIAlertAction.init(title: "删除", style: .cancel) { (UIAlertAction) in
    print("放弃选择")
}
let SureAction = UIAlertAction.init(title: "确定", style: .destructive) { (UIAlertAction) in
    print("确定选择")
}
/**
 添加到提示上
 */
NwAlertView.addAction(DeleteAction)
NwAlertView.addAction(SureAction)

3、进行弹出展示

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

展示的效果图如下:
有两种形式

Simulator Screen Shot 2017年6月20日 上午10.30.38.png
Simulator Screen Shot 2017年6月20日 上午10.31.26.png

4、友情链接之老版本的提示写法

 let oldAlertView = UIAlertView.init(title: "美女", message: "请选择你心仪的哪一位", delegate: self, cancelButtonTitle: "取消")
 oldAlertView.addButton(withTitle: "确定")
 oldAlertView.show()

代理事件

// TODO: 老的写法的代理事件
func alertView(_ alertView: UIAlertView, clickedButtonAt buttonIndex: Int) {
     print("我选择"+"\(buttonIndex)")
}

三、UIAlertController---> ActionSheet 的使用和方法

1、初始化

 NwAlertView = UIAlertController.init(title: "选一选", message: "你挑选哪一位美女?", preferredStyle: .actionSheet)

2、添加交互

/**
 添加其他按钮
 */
let Action1 = UIAlertAction.init(title: "貂蝉", style: .default) { (UIAlertAction) in
     print("我选择貂蝉")
}
NwAlertView.addAction(Action1)
let Action2 = UIAlertAction.init(title: "西施", style: .default) { (UIAlertAction) in
    print("我选择西施")
}
NwAlertView.addAction(Action2)

/**
 添加清楚按钮
 */
let DeleteAction = UIAlertAction.init(title: "取消", style: .cancel) { (DeleteAction) in
    
}
/**
 添加到提示窗上
 */
NwAlertView.addAction(DeleteAction)

3、渲染到视图上

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

效果展示图:

Simulator Screen Shot 2017年6月20日 上午10.30.10.png
Simulator Screen Shot 2017年6月20日 上午10.27.36.png

4、友情链接之老 ActionSheet的创建

 let oldActionSheet = UIActionSheet.init(title: "挑美女", delegate: self, cancelButtonTitle: "取消", destructiveButtonTitle: "选定")
 oldActionSheet.addButton(withTitle: "就是你吧")
 oldActionSheet.show(in: self.view)

代理事件

func actionSheet(_ actionSheet: UIActionSheet, clickedButtonAt buttonIndex: Int) {
     print("我的选择ActionSheet"+"\(buttonIndex)")
}

效果展示:

Simulator Screen Shot 2017年6月20日 上午10.47.52.png
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容