//
// 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...