當(dāng)前位置:博客首頁>>swift >> 閱讀正文

swift3中提示框UIAlertController的使用

作者: 鄭曉 分類: swift 發(fā)布于: 2017-03-21 18:30 瀏覽:5,218 沒有評論


在xcode8 swift3中已經(jīng)已經(jīng)棄用了UIAlertView控件,統(tǒng)一使用UIAlertController代替,以下是UIAlertController的簡單示例。


IBAction func myBtn(_ sender: Any) {
let alertController = UIAlertController(title: "Demo", message: "確定還是取消", preferredStyle: .actionSheet)
//定義取消按鈕及事件
let cancelAction = UIAlertAction(title: "取消", style: .cancel, handler: {
(UIAlertAction)->Void in
print("你點(diǎn)擊了取消按鈕")
//其它功能
})
//定義確定按鈕及事件
let okAction = UIAlertAction(title: "確定", style: .default, handler:{
(UIAlertAction) -> Void in
print("點(diǎn)擊確定事件")
//其它功能
})
//將定義的按鈕添加到controller中
alertController.addAction(cancelAction)
alertController.addAction(okAction)

self.present(alertController, animated: true, completion: nil)
}

其中preferredStyle可以是.alert 會在屏幕中心彈出,如果是.actionSheet 則是從底部彈出。
handler事件如果不需要可以設(shè)置為nil。

? ? ? ?

本文采用知識共享署名-非商業(yè)性使用 3.0 中國大陸許可協(xié)議進(jìn)行許可,轉(zhuǎn)載時請注明出處及相應(yīng)鏈接。

本文永久鏈接: http://www.yjfs.org.cn/swift3-uialertcontroller.html

發(fā)表評論

change vcode