在Swift中獲取iOS警告框TextField的輸入值
通常,我們使用UIAlertController來顯示帶有關閉操作按鈕的警告框。但是UIAlertController 提供了更多靈活性來向警告框新增UITextFields。讓我們看看一些不同用例的示例。
當您向UIAlertController新增文字欄位時,您可以獲取在文字欄位中輸入的值。也可以新增多個文字欄位。
在這個例子中,我們將使用以下步驟來獲取iOS文字欄位的輸入值。
基本設定
在此步驟中,您將向控制器的檢視新增一個按鈕物件,以便透過單擊它來呈現警告控制器。這是基本設定程式碼:
示例
import UIKit
class TestController: UIViewController {
private let clickButton = UIButton()
override func viewDidLoad() {
super.viewDidLoad()
initialSetup()
}
private func initialSetup() {
// basic setup
view.backgroundColor = .white
navigationItem.title = "UIAlertController"
// button customization
clickButton.backgroundColor = .darkGray
clickButton.setTitle("Show Alert", for: .normal)
clickButton.setTitleColor(.white, for: .normal)
clickButton.layer.cornerRadius = 8
clickButton.addTarget(self, action: #selector(handleClickButtonTapped), for: .touchUpInside)
// adding constraints to the button
view.addSubview(clickButton)
clickButton.translatesAutoresizingMaskIntoConstraints = false
clickButton.heightAnchor.constraint(equalToConstant: 50).isActive = true
clickButton.widthAnchor.constraint(equalToConstant: 250).isActive = true
clickButton.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
clickButton.centerYAnchor.constraint(equalTo: view.centerYAnchor).isActive = true
}
@objc private func handleClickButtonTapped() {
}
}
輸出
在上面的程式碼中,我們添加了一個帶有目標動作方法的按鈕。在下一步中,我們將實現handleClickButtonTapped方法來顯示警告。
顯示基本警告
在此步驟中,我們將顯示一個帶有標題和訊息的簡單警告。此外,我們還將新增一些操作來關閉警告。
示例
@objc private func handleClickButtonTapped() {
// creating an alert controller object
let alertController = UIAlertController(title: "Login", message: "Are you sure that you want to login?", preferredStyle: .alert)
// adding actions to alert
alertController.addAction(UIAlertAction(title: "Yes", style: .default))
alertController.addAction(UIAlertAction(title: "Not now", style: .default))
// present the alert on the screen
self.present(alertController, animated: true)
}
輸出
輸入電子郵件地址
在此步驟中,我們將新增一個文字欄位來輸入電子郵件地址並獲取其值。程式碼如下:
示例
@objc private func handleClickButtonTapped() {
// creating an alert controller object
let alertController = UIAlertController(title: "Enter Email", message: "Please enter a valid email address.", preferredStyle: .alert)
// adding a textField for an email address
alertController.addTextField { textField in
// configure the text field
textField.placeholder = "Email Address"
textField.keyboardType = .emailAddress
textField.autocorrectionType = .no
}
// creating OK action
let okAction = UIAlertAction(title: "OK", style: .default) {
action in guard let textField = alertController.textFields?.first,
let text = textField.text else {
print("No value has been entered in email address")
return
}
print("Entered email address value: \(text)")
}
// creating a Cancel action
let cancelAction = UIAlertAction(title: "Cancel", style: .cancel, handler: nil)
// adding actions to alert
alertController.addAction(okAction)
alertController.addAction(cancelAction)
// showing the alert
present(alertController, animated: true, completion: nil)
}
輸出
這是在控制檯上列印的輸出
Entered email address value: test@gmail.com
在這個例子中,我們建立了一個帶有文字欄位和兩個操作(確定和取消)的警告控制器。當用戶點選“確定”按鈕時,將呼叫處理程式塊,我們可以使用alertController.textFields?.first?.text訪問文字欄位的值。然後,我們使用文字的值來執行我們需要對輸入執行的任何操作。
新增多個文字欄位
在此步驟中,我們將向警告框新增兩個文字欄位。第一個文字欄位用於姓名,第二個文字欄位用於電子郵件地址。程式碼如下:
示例
@objc private func handleClickButtonTapped() {
// creating an alert controller object
let alertController = UIAlertController(title: "Enter Details", message: "Please enter your full name & a valid email address.", preferredStyle: .alert)
// adding a textField for name
alertController.addTextField { textField in
textField.placeholder = "Full Name"
textField.autocorrectionType = .no
textField.autocapitalizationType = .words
}
// adding a textField for email address
alertController.addTextField { textField in
textField.placeholder = "Email Address"
textField.keyboardType = .emailAddress
textField.autocorrectionType = .no
}
// creating OK action
let okAction = UIAlertAction(title: "OK", style: .default) { action in
guard let nameTextField = alertController.textFields?[0],
let emailTextField = alertController.textFields?[1],
let name = nameTextField.text,
let email = emailTextField.text else {
return
}
print("Entered name: \(name) and email address: \(email)")
}
// creating Cancel action
let cancelAction = UIAlertAction(title: "Cancel", style: .cancel, handler: nil)
// adding actions to alert
alertController.addAction(okAction)
alertController.addAction(cancelAction)
// showing the alert
present(alertController, animated: true, completion: nil)
}
輸出
這是在控制檯上列印的輸出
Entered name: Alex Murphy and email address: alexmurphy@gmail.com
在這個例子中,我們的警告框中有兩個文字欄位,我們使用它們在textFields陣列中的相應索引來訪問它們的值。
結論
總而言之,使用Swift在iOS警告框中從TextField獲取輸入資料非常簡單。還有其他方法可以調整輸入,例如新增更多文字欄位、啟用安全文字輸入或將其限制為僅數字。您可以使用UIAlertController的處理程式引數檢索文字欄位的值。您可以使用這些方法為您的iOS應用程式設計適應性和吸引人的使用者介面。
資料結構
網路
關係資料庫管理系統 (RDBMS)
作業系統
Java
iOS
HTML
CSS
Android
Python
C語言程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP