📅  最后修改于: 2023-12-03 14:48:09.109000             🧑  作者: Mango
As an iOS developer, you may need to display a message to your users and ask for their confirmation. In such cases, you can make use of the UIAlertController class to create a pop-up message box with Yes and No buttons.
let alertController = UIAlertController(title: "Title", message: "Message", preferredStyle: .alert)
let yesAction = UIAlertAction(title: "Yes", style: .default) { (_) in
// Perform action if Yes button is tapped
}
let noAction = UIAlertAction(title: "No", style: .destructive) { (_) in
// Perform action if No button is tapped
}
alertController.addAction(yesAction)
alertController.addAction(noAction)
present(alertController, animated: true, completion: nil)
Here, we first create an instance of UIAlertController with the given title and message, center aligned. Then, we define two actions, one for Yes and the other for No, with their respective titles and styles. Finally, we add these actions to the alertController and present the alert to the user.
You can customize the look and feel of the UIAlertController by setting its properties. For example, you can change the font, color, and alignment of the title and message.
let alertController = UIAlertController(title: "Title", message: "Message", preferredStyle: .alert)
let yesAction = UIAlertAction(title: "Yes", style: .default) { (_) in
// Perform action if Yes button is tapped
}
let noAction = UIAlertAction(title: "No", style: .destructive) { (_) in
// Perform action if No button is tapped
}
alertController.addAction(yesAction)
alertController.addAction(noAction)
alertController.view.tintColor = UIColor.red
alertController.view.backgroundColor = UIColor.white
let titleFont = [NSAttributedString.Key.font: UIFont.boldSystemFont(ofSize: 18), NSAttributedString.Key.foregroundColor: UIColor.black]
let messageFont = [NSAttributedString.Key.font: UIFont.systemFont(ofSize: 14), NSAttributedString.Key.foregroundColor: UIColor.gray]
let titleAttributed = NSMutableAttributedString(string: "Title", attributes: titleFont)
let messageAttributed = NSMutableAttributedString(string: "Message", attributes: messageFont)
alertController.setValue(titleAttributed, forKey: "attributedTitle")
alertController.setValue(messageAttributed, forKey: "attributedMessage")
present(alertController, animated: true, completion: nil)
In this example, we have changed the tint color of the alertController to red and its background color to white. We have also customized the font and color of the title and message. We use NSAttributedString to create the attributed strings for title and message, which are then set as the attributedTitle and attributedMessage of the alertController.
UIAlertController provides a simple and effective way to ask for user confirmation or display messages in your iOS app. By using Yes and No options, you can make it easy for users to interact with your app. With a little bit of customization, you can improve the look and feel of your alert messages and make them more user-friendly.
Happy coding!