Important: Uialertview is deprecated in IOS 8. (Note that uialertviewdelegate is also deprecated.) To create and manage alerts in IOS 8 and later, instead use Uialertcontroller with a preferredstyle ofuialertcontrollersty Lealert.
Using Uialertview in Xcode7 will report the following warning:
' Uialertview ' was deprecated on IOS 9.0:uialertview is deprecated. Use Uialertcontroller with a preferredstyle of Uialertcontrollerstylealert instead
Alert Views:alert views display a concise and informative alert message to the user.
Uialertcontroller also replaces Uialertview and Uiactionsheet, unifying the concept of alert at the system level-either in modal or PopOver mode.
?
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859 |
// ViewController.swift
import UIKit
class
ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
//创建一个Button
let button = UIButton(type: UIButtonType.Custom)
//初始化UIButton
button.frame = CGRectMake(50, 100, 150, 50)
//创建一个CGRect, 设置位置和大小
button.backgroundColor = UIColor.greenColor()
//设置背景色
button.setTitle(
"点击显示弹窗"
, forState: UIControlState.Normal)
//设置标题
//传递触摸对象(点击事件)
button.addTarget(self, action:
"buttonPressed:"
, forControlEvents: UIControlEvents.TouchUpInside)
self.view.addSubview(button)
}
// Action
func buttonPressed(sender: UIButton) {
showAlertReset()
}
func showAlertDefault(){
let alertController = UIAlertController(title:
"弹窗标题"
, message:
"Hello, 这个是UIAlertController的默认样式"
, preferredStyle: UIAlertControllerStyle.Alert)
let cancelAction = UIAlertAction(title:
"取消"
, style: UIAlertActionStyle.Cancel, handler: nil)
let okAction = UIAlertAction(title:
"好的"
, style: UIAlertActionStyle.Default, handler: nil)
let resetAction = UIAlertAction(title:
"重置"
, style: UIAlertActionStyle.Destructive, handler: nil)
alertController.addAction(resetAction)
alertController.addAction(cancelAction)
alertController.addAction(okAction)
self.presentViewController(alertController, animated:
true
, completion: nil)
}
func showAlertReset(){
let alertControl = UIAlertController(title:
"弹窗的标题"
, message:
"Hello,showAlertReset "
, preferredStyle: UIAlertControllerStyle.Alert)
let cancelAction = UIAlertAction(title:
"取消操作"
, style: UIAlertActionStyle.Destructive, handler: nil)
let okAction = UIAlertAction(title:
"好的"
, style: UIAlertActionStyle.Default, handler: nil)
alertControl.addAction(cancelAction)
alertControl.addAction(okAction)
self.presentViewController(alertControl, animated:
true
, completion: nil)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
|
Reference:
Http://www.jianshu.com/p/86f933850df8
http://blog.csdn.net/xiaowenwen1010/article/details/40108097
[Swift] Uikit Learning Warning Box: Uialertcontroller and Uialertview