Swift 顯示警告訊息有兩種方式
解決方案來源:
iOS - 客製化Dialog
PopupDialog:採用Swif編寫的一個簡單、可定製的iOS彈出對話框
UIAlertController 顯示警告訊息和選單
方式一
使用 UIAlertController
使用 CustomAlertController範例
UIAlertController 教程:讓你輕鬆在 UIViewController 以外的地方呈現警告
Making an Alert in iOS
方式二
使用 PopupDialog
import PopupDialog func ShowMessageBoxDlg( nTitle:Int, nMessage:Int) { let Title = NSLocalizedString( String(nTitle), comment: "") let Message = NSLocalizedString( String(nMessage), comment: "") let PopupDlg = PopupDialog( title: Title, message: Message, buttonAlignment: .horizontal, transitionStyle: .zoomIn, tapGestureDismissal: true, panGestureDismissal: true, hideStatusBar: true) { } let MagOk = NSLocalizedString( "Ok", comment: "") let buttonOk = DefaultButton(title: MagOk) { } PopupDlg.addButtons([buttonOk]) self.present( PopupDlg, animated: true, completion: nil) }
方式三
自行用 UIViewController 設計一個 Dialog
import UIKit //------------------------------------------------------------------- var bShowWaitDialogClose: Bool = false //------------------------------------------------------------------- class TShowWaitDialogVc: UIViewController { var nCheckCloseDialogTimer:Timer? var sTitleStr:String = "" var sMessageStr:String = "" var activityIndicator:UIActivityIndicatorView = UIActivityIndicatorView() @IBOutlet weak var ViewDialog: UIView! @IBOutlet weak var LabelTitleStr: UILabel! @IBOutlet weak var LabelMsgStr: UILabel! //------------------------------------------------------------------- override func viewDidLoad() { super.viewDidLoad() bShowWaitDialogClose = false let yourColor : UIColor = UIColor(red:170.0/255.0, green:170.0/255.0, blue:170.0/255.0, alpha: 1.0 ) ViewDialog.layer.masksToBounds = true ViewDialog.layer.borderColor = yourColor.cgColor ViewDialog.layer.cornerRadius = 30 ViewDialog.layer.borderWidth = 2 ViewDialog.layer.shadowOffset = CGSize(width: 5, height: 5) ViewDialog.layer.shadowOpacity = 0.7 ViewDialog.layer.shadowRadius = 5 ViewDialog.layer.shadowColor = UIColor(red:170.0/255.0, green:170.0/255.0, blue:170.0/255.0, alpha: 1.0 ).cgColor LabelTitleStr.text = sTitleStr LabelMsgStr.text = sMessageStr CheckCloseDialogTimerInit() ActivityIndicatorStart() } //------------------------------------------------------------------- func CheckCloseDialogTimerInit() { nCheckCloseDialogTimer = Timer.scheduledTimer(timeInterval: 0.2, target: self, selector:#selector(self.CheckCloseDialogTimerDown), userInfo: nil, repeats: true) } //------------------------------------------------------------------- @objc func CheckCloseDialogTimerDown() { if( bShowWaitDialogClose == true) { self.nCheckCloseDialogTimer?.invalidate() ActivityIndicatorStop() dismiss(animated: true, completion: nil) } } //------------------------------------------------------------------- @objc func ActivityIndicatorStart() { activityIndicator.center = self.view.center activityIndicator.hidesWhenStopped = false activityIndicator.style = UIActivityIndicatorView.Style.gray activityIndicator.isHidden = false view.addSubview(activityIndicator) activityIndicator.startAnimating() UIApplication.shared.beginIgnoringInteractionEvents() } //------------------------------------------------------------------- @objc func ActivityIndicatorStop() { self.activityIndicator.stopAnimating() self.activityIndicator.isHidden = true UIApplication.shared.endIgnoringInteractionEvents() } //------------------------------------------------------------------- } //-------------------------------------------------------------------
func ShowWaitDialogVcShow() { bShowWaitDialogClose = false let storyBoard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil) let ShowWaitDialogVc = storyBoard.instantiateViewController(withIdentifier: "ShowWaitDialogVcID") as! TShowWaitDialogVc ShowWaitDialogVc.sTitleStr = "Processing" ShowWaitDialogVc.sMessageStr = "In Processing, Please Wait..." self.present( ShowWaitDialogVc, animated: true, completion: nil) }
func BtnCloseDialg(_ sender: Any) { print("FUNC: \(#file), \(#function)") bShowWaitDialogClose = true }
var hBtnTouchTimer:Timer? @IBAction func BtnTestOnClick(_ sender: Any) { bShowWaitDialogClose = false hBtnTouchTimer = Timer.scheduledTimer( timeInterval: 20, target: self, selector:#selector( self.BtnTouchTimerDown2), userInfo: nil, repeats: false) ShowWaitDialogVcShow() } @objc func BtnTouchTimerDown2() { if( bShowWaitDialogClose == false) bShowWaitDialogClose = true }
沒有留言:
張貼留言