★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公衆號:山青詠芝(shanqingyongzhi)
➤博客園地址:山青詠芝(https://www.cnblogs.com/strengthen/)
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址:http://www.javashuo.com/article/p-dbjvamrg-hr.html
➤若是連接不是山青詠芝的博客園地址,則多是爬取做者的文章。
➤原文已修改更新!強烈建議點擊原文地址閱讀!支持做者!支持原創!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★html
目錄:[Swift]通天遁地Swiftgit
本文將演示一款很是強大的警告窗口。github
Github地址:【SweetAlert】swift
下載所需的第三方類庫。在下載的文件夾中,選擇:微信
【SweetAlert】->將【SweetAlert.swift】文件拖動到項目中ide
->在彈出的添加文件窗口中,保持默認的設置選項->【Finish】工具
在項目導航區,打開視圖控制器的代碼文件【ViewController.swift】post
如今編寫代碼,建立一個警告窗口窗口。spa
1 import UIKit 2 3 class ViewController: UIViewController { 4 5 //添加一個警告窗口,做爲當前類的一個屬性 6 var alert = SweetAlert() 7 override func viewDidLoad() { 8 super.viewDidLoad() 9 // Do any additional setup after loading the view, typically from a nib. 10 11 //初始化一個按鈕,當用戶點擊該按鈕時,彈出一個警告窗口 12 let popup = UIButton(frame: CGRect(x: 0, y: 0, width: 280, height: 40)) 13 //將按鈕控件放置在根視圖的中心位置 14 popup.center = self.view.center 15 //設置按鈕控件的背景顏色爲橙色 16 popup.backgroundColor = UIColor.orange 17 //設置按鈕控件在正常狀態下的標題文字 18 popup.setTitle("SweetAlert", for: .normal) 19 //給按鈕控件綁定點擊事件 20 popup.addTarget(self, 21 action: #selector(ViewController.cancelAndConfirm(_:)), 22 for: .touchUpInside) 23 24 //設置根視圖的背景顏色爲橙色 25 self.view.backgroundColor = UIColor.orange 26 //並將按鈕添加到根視圖中 27 self.view.addSubview(popup) 28 } 29 30 //1.添加一個方法,用來響應按鈕的點擊事件 31 func aBasicMessageAlert(_ sender: AnyObject) 32 { 33 //當按鈕被點擊時,初始化並彈出一個警告窗口,同時設置窗口中的文字信息。 34 _ = SweetAlert().showAlert("Here's a message!") 35 } 36 37 //2.對代碼進行一些修改 38 //添加一個方法,用來響應按鈕的點擊事件,記得修改按鈕的方法綁定語句 39 func subtitleAlert(_ sender: AnyObject) { 40 //初始化並彈出一個警告窗口,同時設置窗口中的標題、子標題和樣式 41 _ = SweetAlert().showAlert("Here's a message!", //標題 42 subTitle: "It's pretty, isn't it?",//子標題 43 style: AlertStyle.none)//樣式 44 } 45 46 //3.對代碼進行一些修改 47 //添加一個方法,用來響應按鈕的點擊事件,記得修改按鈕的方法綁定語句 48 func sucessAlert(_ sender: AnyObject) 49 { 50 //初始化並彈出一個警告窗口,同時設置窗口中的標題、子標題,並將窗口樣式設置爲成功樣式。 51 _ = SweetAlert().showAlert("Good job!", 52 subTitle: "You clicked the button!", 53 style: AlertStyle.success) 54 } 55 56 //4.對代碼進行一些修改 57 //添加一個方法,該方法用來建立帶有多個按鈕的彈出窗口,記得修改按鈕的方法綁定語句 58 func cancelAndConfirm(_ sender: AnyObject) 59 { 60 //初始化並彈出一個警告窗口,同時設置窗口中的標題、子標題和樣式。 61 //並添加了兩個不一樣外觀樣式的按鈕。 62 _ = SweetAlert().showAlert("Are you sure?", 63 subTitle: "You file will permanently delete!", 64 style: AlertStyle.warning, 65 buttonTitle:"No, cancel plx!", 66 buttonColor:UIColor.colorFromRGB(0xD0D0D0) , 67 otherButtonTitle: "Yes, delete it!", 68 otherButtonColor: UIColor.colorFromRGB(0xDD6B55)) 69 //添加一條語句,用來響應按鈕被點擊的事件。 70 { (isOtherButton) -> Void in 71 if isOtherButton == true 72 { 73 //當第二個按鈕被點擊時,彈出另外一個錯誤類型的警告窗口。 74 _ = SweetAlert().showAlert("Cancelled!", subTitle: "Your imaginary file is safe", style: AlertStyle.error) 75 } 76 else 77 { 78 //當第一個按鈕被點擊時,彈出一個成功類型的警告窗口。 79 _ = SweetAlert().showAlert("Deleted!", subTitle: "Your imaginary file has been deleted!", style: AlertStyle.success) 80 } 81 } 82 } 83 84 //5.添加一個方法,該方法用來建立擁有自定義圖標的窗口,記得修改按鈕的方法綁定語句。 85 func customIconAlert(_ sender: AnyObject) 86 { 87 //建立一個自定義圖標樣式的窗口,圖標時項目中的一張圖片。 88 _ = SweetAlert().showAlert("Sweet!", 89 subTitle: "Here's a custom image.", 90 style: AlertStyle.customImag(imageFile: "coffee.png")) 91 } 92 93 override func didReceiveMemoryWarning() { 94 super.didReceiveMemoryWarning() 95 // Dispose of any resources that can be recreated. 96 } 97 }