//
// ViewController.swift
// Swift-普通警告框的使用
//
// Created by 品德信息 on 2016/12/28.
// Copyright © 2016年 品德信息. All rights reserved.
//
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
//基本警告框的使用
self.baseAlertController()
//警告框的动作列表样式
self.actionSheetController()
}
func actionSheetController() {
let btn = UIButton(type:UIButtonType.system)
btn.frame = CGRect(x:20,y:220,width:200,height:44)
btn.setTitle("Question", for: UIControlState())
btn.addTarget(self, action: #selector(ViewController.showActionSheet), for: UIControlEvents.touchUpInside)
btn.backgroundColor = UIColor.lightGray
self.view.addSubview(btn)
}
func showActionSheet() {
let alert = UIAlertController(title:"Infomation",message:"你最喜欢干什么?",preferredStyle:UIAlertControllerStyle.actionSheet)
let yes = UIAlertAction(title:"🐟",style:UIAlertActionStyle.default,handler:{(alerts:UIAlertAction) -> Void in
//暂时理解为回调
print("我喜欢吃鱼")
})
// public convenience init(title: String?, style: UIAlertActionStyle, handler: ((UIAlertAction) -> Swift.Void)? = nil)
let no = UIAlertAction(title:"hunting",style:UIAlertActionStyle.destructive,handler:{(alerts:UIAlertAction) -> Void in
print("i like hunting")})
let unKnown = UIAlertAction(title:"undo" ,style:UIAlertActionStyle.cancel,handler:{(alerts:UIAlertAction) -> Void in
print("不知道什么操作")
})
alert.addAction(yes)
alert.addAction(no)
alert.addAction(unKnown)
self.present(alert,animated: true,completion: nil)
}
func baseAlertController() {
let btn = UIButton(type:UIButtonType.system)
btn.frame = CGRect(x:20,y:120,width:200,height:44)
btn.setTitle("Question", for: UIControlState())
btn.addTarget(self, action: #selector(ViewController.alert), for: UIControlEvents.touchUpInside)
btn.backgroundColor = UIColor.lightGray
self.view.addSubview(btn)
}
func alert() {
let alert = UIAlertController(title:"Infomation",message:"Are you a student ?",preferredStyle:UIAlertControllerStyle.alert)
let yes = UIAlertAction(title:"Yes",style:UIAlertActionStyle.default,handler:{(alerts:UIAlertAction) -> Void in
//暂时理解为回调
print("yes ,I'm a student")
})
let no = UIAlertAction(title:"No",style:UIAlertActionStyle.default,handler:{(alerts:UIAlertAction) -> Void in
print("No,I'm not a student")})
let unKnown = UIAlertAction(title:"undo" ,style:UIAlertActionStyle.default,handler:{(alerts:UIAlertAction) -> Void in
print("不知道什么操作")
})
alert.addAction(yes)
alert.addAction(no)
alert.addAction(unKnown)
self.present(alert,animated: true,completion: nil)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
swift-UIAlertController
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- 在iOS8以后用于替代UIAlertView和UIActionSheet 一. 菜单样式 UIAlertContr...
- let alert = UIAlertController(title: "温馨提示", message: "1....
- 三种状态 常规(default)、取消(cancel)以及警示(destructive) 1,在中间有点击方法...
- let alert = UIAlertController.init(title: "温馨提示", message...
- // 定义button let centerBtn = UIButton(frame:CGRectMake(vi...