Search code examples
swiftfor-loopif-statementuialertcontrolleruialertaction

How do you use for loop to test code in UIAlertAction


I am trying to use UIAlertAction to have a user enter in a name and check that name against an array of names. If the name exists I want to notify the user and return to the UIAlertcontroller. If the name does not exist I want some other code to be run. I added in a for loop for this but i cant seem to stop the other part of code from running regardless if the for loop alert pops up.

    @IBAction func savedGamePressed(_ sender: UIButton) {
        var textField = UITextField()
        
        let alert = UIAlertController(title: "Please Name This Game", message: "", preferredStyle: .alert)
        
        let action = UIAlertAction(title: "Save Game", style: .default){ [self](action) in
            //what happens once the user clicks the saved game button
            newGameName = textField.text!
            
            for game in SingletonClass.shared.gamesArray where newGameName == game.gameName {
                let alert = UIAlertController(title: "Name Already Used", message: "", preferredStyle: .alert)
                
                let action = UIAlertAction(title: "OK", style: .default){_ in
                    alert.dismiss(animated: false)
                    
                    self.used = false
                }
                alert.addAction(action)
                
                present(alert, animated: true, completion: nil)
                
            }
            // stop code here if for loop alert pops up
            if used != false{
                let newGame = Games(context: SingletonClass.shared.context)
                newGame.gameName = newGameName
                newGame.gameSaved = true
                newGame.gameRounds = SingletonClass.shared.numberRounds[0].nRounds

I've tried a combo of for loops and if statements. Not sure if this is even the best way to do it. I'm new to programming and open to any suggestions/help.


Solution

  • Don't use a for loop. Just check if the new name is in the list of existing names.

        let action = UIAlertAction(title: "Save Game", style: .default){ [self](action) in
            //what happens once the user clicks the saved game button
            newGameName = textField.text!
                
            if SingletonClass.shared.gamesArray.firstIndex(where: { newGameName == $0.gameName }) != nil {
                let alert = UIAlertController(title: "Name Already Used", message: "", preferredStyle: .alert)
                    
                let action = UIAlertAction(title: "OK", style: .default)
                alert.addAction(action)
                    
                present(alert, animated: true, completion: nil)
            } else {
                let newGame = Games(context: SingletonClass.shared.context)
                newGame.gameName = newGameName
                newGame.gameSaved = true
                newGame.gameRounds = SingletonClass.shared.numberRounds[0].nRounds
    

    Also note that an alert action's handler is only called when the alert is dismissed so you do not need to explicitly call dismiss on a UIAlertController in an alert action's handler.