我如何在Swift中創(chuàng)建UIAlertView?我一直在努力在Swift中創(chuàng)建一個(gè)UIAlertView,但由于某種原因,我無法正確地說出這句話,因?yàn)槲沂盏搅诉@個(gè)錯(cuò)誤:找不到接受提供的參數(shù)的'init'的重載這是我寫的方式:let button2Alert: UIAlertView = UIAlertView(title: "Title", message: "message",
delegate: self, cancelButtonTitle: "OK", otherButtonTitles: nil)然后打電話給我我正在使用:button2Alert.show()截至目前它正在崩潰,我似乎無法使語(yǔ)法正確。
4 回答

梵蒂岡之花
TA貢獻(xiàn)1900條經(jīng)驗(yàn) 獲得超5個(gè)贊
在Swift 4.2和Xcode 10中
方法1:
簡(jiǎn)單的警告
let alert = UIAlertController(title: "Your title", message: "Your message", preferredStyle: .alert) let ok = UIAlertAction(title: "OK", style: .default, handler: { action in }) alert.addAction(ok) let cancel = UIAlertAction(title: "Cancel", style: .default, handler: { action in }) alert.addAction(cancel) DispatchQueue.main.async(execute: { self.present(alert, animated: true)})
方法2:
警告共享類別
如果你想要共享類的風(fēng)格(寫一次使用的地方)
import UIKitclass SharedClass: NSObject {//This is shared classstatic let sharedInstance = SharedClass() //Show alert func alert(view: UIViewController, title: String, message: String) { let alert = UIAlertController(title: title, message: message, preferredStyle: .alert) let defaultAction = UIAlertAction(title: "OK", style: .default, handler: { action in }) alert.addAction(defaultAction) DispatchQueue.main.async(execute: { view.present(alert, animated: true) }) } private override init() { }}
現(xiàn)在在每件商品中都會(huì)調(diào)出這樣的提醒
SharedClass.SharedInstance.alert(view: self, title: "Your title here", message: "Your message here")
方法3:
現(xiàn)在警告所有WINDOWS
如果要在所有視圖之上顯示警報(bào),請(qǐng)使用此代碼
func alertWindow(title: String, message: String) { DispatchQueue.main.async(execute: { let alertWindow = UIWindow(frame: UIScreen.main.bounds) alertWindow.rootViewController = UIViewController() alertWindow.windowLevel = UIWindowLevelAlert + 1 let alert2 = UIAlertController(title: title, message: message, preferredStyle: .alert) let defaultAction2 = UIAlertAction(title: "OK", style: .default, handler: { action in }) alert2.addAction(defaultAction2) alertWindow.makeKeyAndVisible() alertWindow.rootViewController?.present(alert2, animated: true, completion: nil) })}
功能調(diào)用
SharedClass.sharedInstance.alertWindow(title:"This your title", message:"This is your message")
方法4:
通過擴(kuò)展提醒
extension UIViewController { func showAlert(withTitle title: String, withMessage message:String) { let alert = UIAlertController(title: title, message: message, preferredStyle: .alert) let ok = UIAlertAction(title: "OK", style: .default, handler: { action in }) let cancel = UIAlertAction(title: "Cancel", style: .default, handler: { action in }) alert.addAction(ok) alert.addAction(cancel) DispatchQueue.main.async(execute: { self.present(alert, animated: true) }) }}
現(xiàn)在這樣打電話
//Call showAlert function in your class@IBAction func onClickAlert(_ sender: UIButton) { showAlert(withTitle:"Your Title Here", withMessage: "YourCustomMessageHere")}
方法5:
與TEXTFIELDS一起提醒
如果要添加文本字段以提醒。
//Global variablesvar name:String?var login:String?//Call this function like this: alertWithTF() //Add textfields to alert func alertWithTF() { let alert = UIAlertController(title: "Login", message: "Enter username&password", preferredStyle: .alert) // Login button let loginAction = UIAlertAction(title: "Login", style: .default, handler: { (action) -> Void in // Get TextFields text let usernameTxt = alert.textFields![0] let passwordTxt = alert.textFields![1] //Asign textfileds text to our global varibles self.name = usernameTxt.text self.login = passwordTxt.text print("USERNAME: \(self.name!)\nPASSWORD: \(self.login!)") }) // Cancel button let cancel = UIAlertAction(title: "Cancel", style: .destructive, handler: { (action) -> Void in }) //1 textField for username alert.addTextField { (textField: UITextField) in textField.placeholder = "Enter username" //If required mention keyboard type, delegates, text sixe and font etc... //EX: textField.keyboardType = .default } //2nd textField for password alert.addTextField { (textField: UITextField) in textField.placeholder = "Enter password" textField.isSecureTextEntry = true } // Add actions alert.addAction(loginAction) alert.addAction(cancel) self.present(alert, animated: true, completion: nil)}
方法6:
帶擴(kuò)展的SharedClass中的警報(bào)
//This is your shared classimport UIKit class SharedClass: NSObject { static let sharedInstance = SharedClass() //Here write your code.... private override init() { }}//Alert function in shared classextension UIViewController { func showAlert(title: String, msg: String) { DispatchQueue.main.async { let alert = UIAlertController(title: title, message: msg, preferredStyle: .alert) alert.addAction(UIAlertAction(title: "OK", style: .default, handler: nil)) self.present(alert, animated: true, completion: nil) } }}
現(xiàn)在直接這樣打電話
self.showAlert(title: "Your title here...", msg: "Your message here...")
方法7:
在單獨(dú)的類中使用擴(kuò)展的共享類進(jìn)行警報(bào)提醒。
創(chuàng)建一個(gè)新的Swift類,和import UIKit
。復(fù)制并粘貼下面的代碼。
//This is your Swift new class fileimport UIKitimport Foundationextension UIAlertController { class func alert(title:String, msg:String, target: UIViewController) { let alert = UIAlertController(title: title, message: msg, preferredStyle: UIAlertControllerStyle.alert) alert.addAction(UIAlertAction(title: "Ok", style: UIAlertActionStyle.default) { (result: UIAlertAction) -> Void in }) target.present(alert, animated: true, completion: nil) }}
現(xiàn)在在所有類中調(diào)用這樣的警報(bào)功能(單行)。
UIAlertController.alert(title:"Title", msg:"Message", target: self)
如何....
- 4 回答
- 0 關(guān)注
- 770 瀏覽
添加回答
舉報(bào)
0/150
提交
取消