如何基于变量检查数组内容并通过if语句调用警告窗口?Bingo应用咨询
Hey there! Let's break down your two iOS development questions and walk through solutions step by step.
1. Checking Array Content Against a Variable & Triggering Alert Windows
To check an array based on another variable and show an alert when a condition is met, you can use Swift's array methods combined with UIAlertController. Here are common practical scenarios:
Scenario 1: Check if the array contains the target variable
// Define your array and target variable let targetValue = "Bingo" let wordArray = ["Hello", "Bingo", "World"] // Check if the array includes the target if wordArray.contains(targetValue) { // Create and present the alert let alert = UIAlertController( title: "Match Found!", message: "The array contains your target value: \(targetValue)", preferredStyle: .alert ) alert.addAction(UIAlertAction(title: "Got it", style: .default)) present(alert, animated: true) }
Scenario 2: Custom condition (e.g., elements greater than a threshold)
If you need a more specific check (like elements meeting a criteria tied to your variable), use contains(where:):
let threshold = 10 let numberArray = [5, 12, 8, 15] // Check if any element is above the threshold if numberArray.contains(where: { $0 > threshold }) { let alert = UIAlertController( title: "Condition Met", message: "At least one element in the array is greater than \(threshold)", preferredStyle: .alert ) alert.addAction(UIAlertAction(title: "OK", style: .cancel)) present(alert, animated: true) }
2. Optimizing Bingo App Logic & Implementing createAlert
Your current win condition check works, but we can clean it up and integrate the alert functionality smoothly.
Step 1: Simplify the Win Condition Check
Instead of chaining multiple button checks, store your buttons in an array to make the code scalable and easier to maintain:
// Store your row buttons in an array (define this at the class level) let firstRowButtons = [BOneButton, IOneButton, NOneButton, GOneButton, OOneButton] // Check if all buttons are selected func checkWinCondition() { if firstRowButtons.allSatisfy({ $0.isSelected }) { winConditionLabel.text = "You win!" // Trigger your alert when the win condition hits createAlert(sender: firstRowButtons.first!) } }
Step 2: Implement the createAlert Method
Here's a complete implementation of your createAlert function, including win feedback and a restart option to reset the game:
func createAlert(_ sender: UIButton) { let winAlert = UIAlertController( title: "BINGO!", message: "You've scored a winning row! 🎉", preferredStyle: .alert ) // Add a restart action to reset the game state let restartAction = UIAlertAction(title: "Restart Game", style: .default) { _ in // Reset all buttons to unselected state self.firstRowButtons.forEach { button in button.isSelected = false // Reset button image to its unselected state button.setImage(UIImage(named: "unselected_dot"), for: .normal) } self.winConditionLabel.text = "" } let okAction = UIAlertAction(title: "OK", style: .cancel) winAlert.addAction(restartAction) winAlert.addAction(okAction) present(winAlert, animated: true) }
Step 3: Link Button Taps to State Updates
Make sure your button tap handler toggles selection, swaps images, and triggers the win check every time a button is pressed:
@IBAction func bingoButtonTapped(_ sender: UIButton) { // Toggle the button's selected state sender.isSelected.toggle() // Swap images based on current selection state let selectedImage = UIImage(named: "selected_mark") let unselectedImage = UIImage(named: "unselected_dot") sender.setImage(sender.isSelected ? selectedImage : unselectedImage, for: .normal) // Check for win condition after every tap checkWinCondition() }
内容的提问来源于stack exchange,提问作者Matthew W.

