UIAlertView
UIAlertView可用来显示提示信息,下面我们学习如何使用它
1. UIAlertView的创建
let alertView = UIAlertView()
alertView.delegate = self
alertView.title = "摸着你的良心回答"
alertView.message = "邪恶枫叶帅吗?"
alertView.addButtonWithTitle("帅")
alertView.addButtonWithTitle("帅的冒泡")
alertView.cancelButtonIndex = 0
alertView.show()
看看运行结果就知道上面的代码的意思了,现在运行程序
然后我们在加一个按钮
alertView.addButtonWithTitle("帅的天昏地暗")
运行程序
感觉两个按钮似乎好看那么一丢丢
2.UIAlertView的代理
public protocol UIAlertViewDelegate : NSObjectProtocol {
// Called when a button is clicked. The view will be automatically dismissed after this call returns
@available(iOS, introduced=2.0, deprecated=9.0)
optional public func alertView(alertView: UIAlertView, clickedButtonAtIndex buttonIndex: Int)
// Called when we cancel a view (eg. the user clicks the Home button). This is not called when the user clicks the cancel button.
// If not defined in the delegate, we simulate a click in the cancel button
@available(iOS, introduced=2.0, deprecated=9.0)
optional public func alertViewCancel(alertView: UIAlertView)
@available(iOS, introduced=2.0, deprecated=9.0)
optional public func willPresentAlertView(alertView: UIAlertView) // before animation and showing view
@available(iOS, introduced=2.0, deprecated=9.0)
optional public func didPresentAlertView(alertView: UIAlertView) // after animation
@available(iOS, introduced=2.0, deprecated=9.0)
optional public func alertView(alertView: UIAlertView, willDismissWithButtonIndex buttonIndex: Int) // before animation and hiding view
@available(iOS, introduced=2.0, deprecated=9.0)
optional public func alertView(alertView: UIAlertView, didDismissWithButtonIndex buttonIndex: Int) // after animation
// Called after edits in any of the default fields added by the style
@available(iOS, introduced=2.0, deprecated=9.0)
optional public func alertViewShouldEnableFirstOtherButton(alertView: UIAlertView) -> Bool
}
貌似iOS9.0后UIAlertView就标记为过时了
虽然有很多方法,但是一般就用第一个.
在上面的例子的基础上, 我们实现下面的需求,点击不同的按钮答应不同的文字:
“帅” => “兄弟你的眼睛真是瞎的不行”
“帅的冒泡” => “兄弟好眼光”
“帅的天昏地暗” => “兄弟,你果然是个英雄,因为英雄所见略同”
func alertView(alertView: UIAlertView, clickedButtonAtIndex buttonIndex: Int) {
if buttonIndex == 0 {
print("兄弟你的眼睛真是瞎的不行")
return
}
if buttonIndex == 1 {
print("兄弟好眼光")
return
}
if buttonIndex == 2 {
print("兄弟,你果然是个英雄,因为英雄所见略同")
return
}
}
3. 完整代码
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
let alertView = UIAlertView()
alertView.delegate = self
alertView.title = "摸着你的良心回答"
alertView.message = "邪恶枫叶帅吗?"
alertView.addButtonWithTitle("帅")
alertView.addButtonWithTitle("帅的冒泡")
alertView.addButtonWithTitle("帅的天昏地暗")
alertView.cancelButtonIndex = 0
alertView.show()
}
func alertView(alertView: UIAlertView, clickedButtonAtIndex buttonIndex: Int) {
if buttonIndex == 0 {
print("兄弟你的眼睛真是瞎的不行")
return
}
if buttonIndex == 1 {
print("兄弟好眼光")
return
}
if buttonIndex == 2 {
print("兄弟,你果然是个英雄,因为英雄所见略同")
return
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}