如何在 iOS 中顯示彈窗對話方塊?
如果你正在設計任何 iOS 應用程式,知道如何處理彈窗非常重要。我們將重點介紹如何使用 UIAlertController 顯示彈窗。
要詳細瞭解 UIAlertController,請參閱 − https://developer.apple.com/documentation/uikit/uialertcontroller
在此,我們將建立一個新專案,其中有一個按鈕,輕觸該按鈕,我們將顯示帶有自定義訊息的彈窗。
第 1 步 − 開啟 Xcode → 新專案 → 單檢視應用程式 → 讓我們將其命名為“彈窗”
第 2 步 − 開啟 Main.storyboard 並新增一個按鈕並將其命名為 tap。在 ViewController.swit 中建立該按鈕的 @IBAction 並將其命名為 tap。
顯示彈窗有 3 個步驟。首先是從 UIAlertController 建立彈窗物件。其次,對彈窗物件新增操作,最後,顯示彈窗物件。
第 3 步 − 在 tap 按鈕的 @IBAction 下,在你的按鈕例項中新增以下程式碼。
@IBAction func tap(_ sender: Any) { let uialert = UIAlertController(title: "Welcome", message: "Welcome to my channel. Thanks for watching. Click on Okay to continue", preferredStyle: UIAlertController.Style.alert) uialert.addAction(UIAlertAction(title: "Okay", style: UIAlertAction.Style.default, handler: nil)) self.present(uialert, animated: true, completion: nil) }
第 4 步 − 執行程式碼。
廣告