UIAlertControlle時(shí)IOS的對(duì)話(huà)框控制器(警報(bào)控制器),簡(jiǎn)單使用方法如下:
步驟都一樣,先是創(chuàng)建UIAlertController,然后創(chuàng)建UIAlertAction,再將UIAlertAction添加到UIAlertController中,最后顯示對(duì)話(huà)框。
文本對(duì)話(huà)框:
//創(chuàng)建控制器
let alertController = UIAlertController(title: "Title", message: "Message", preferredStyle: .alert)
//設(shè)置action
let okAction = UIAlertAction(title: "OK", style: .default){
(action) in
print("click OK")
}
let cancelAction = UIAlertAction(title: "Cancel", style: .cancel, handler: nil)
//添加action
alertController.addAction(okAction)
alertController.addAction(cancelAction)
//顯示對(duì)話(huà)框
present(alertController, animated: true, completion: nil)
效果如圖:
帶輸入框的對(duì)話(huà)框
//創(chuàng)建控制器
let alertController = UIAlertController(title: "Enter Text", message: nil, preferredStyle: .alert)
//設(shè)置輸入框
alertController.addTextField { (textField) in
textField.placeholder = "Enter text"
}
//設(shè)置action
let cancelAction = UIAlertAction(title: "Cancel", style: .cancel, handler: nil)
let submitAction = UIAlertAction(title: "Submit", style: .default) { (action) in
if let text = alertController.textFields?.first?.text {
print("Entered text: \(text)")
}
}
//添加action
alertController.addAction(cancelAction)
alertController.addAction(submitAction)
//顯示對(duì)話(huà)框
present(alertController, animated: true, completion: nil)
效果如圖:
底部選擇對(duì)話(huà)框
注意preferredStyle為.actionSheet文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-802277.html
//創(chuàng)建控制器
let alertController = UIAlertController(title: "Choose Option", message: nil, preferredStyle: .actionSheet)
//設(shè)置action
let option1Action = UIAlertAction(title: "Option 1", style: .default) { (action) in
print("Option 1 selected")
}
let option2Action = UIAlertAction(title: "Option 2", style: .default) { (action) in
print("Option 2 selected")
}
let cancelAction = UIAlertAction(title: "Cancel", style: .cancel, handler: nil)
//添加action
alertController.addAction(option1Action)
alertController.addAction(option2Action)
alertController.addAction(cancelAction)
//顯示對(duì)話(huà)框
present(alertController, animated: true, completion: nil)
效果如圖:文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-802277.html
到了這里,關(guān)于IOS-UIAlertController簡(jiǎn)單使用-Swift的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!