2

I've got a string array called languages, and I want to create as many UIAlertActions in a UIAlertController as there are elements in the array. I don't know how large the array will be, as the user can add languages to the array using the add option from the same UIAlertController.

loadLanguages() successfully loads userDefaults data into the languages array if there's any existing languages that've either been saved as the 2 starter languages on first app load and/or added by the user on subsequent uses of the app.

The add language option works, and is stored in userDfeaults (self.saveLanguages), and appended to the languages array.

However I'm not sure about creating UIAlertAction options for each language that is in the languages array. I've tried looping through the array to generate each menu item, as the languages array has the answer as to how many UIAlertActions should be displayed, but nothing appears.

After extensive searches I haven't come across anything covering this, but I'm sure there's an elegant approach.

FYI: languageChoiceButton is declared as:

var languageChoiceButton = UIAlertAction()

@objc func languageMenu(){

    loadLanguages()

    let chooseLanguageController = UIAlertController(title: "Vocabulary Tutor", message: "Choose a Language", preferredStyle: .actionSheet)

    let addLanguage = UIAlertAction(title: "Add Language", style: .default, handler: { (action) -> Void in
        let ac = UIAlertController(title: "Add a language", message: nil, preferredStyle: .alert)

        ac.addTextField { textField in
            textField.placeholder = "New language"
        }
        let submitAction = UIAlertAction(title: "Add", style: .default) { [unowned self, ac] (action: UIAlertAction!) in
            self.newLanguage = ac.textFields?[0].text ?? ""
            print("newLanguage: \(self.newLanguage)")
            self.languages.append(self.newLanguage)
            self.saveLanguages()
            self.loadLanguages()

        }
        ac.addAction(submitAction)
        ac.addAction(UIAlertAction(title: "Cancel", style: .cancel))

        self.present(ac, animated: true)
    })

    chooseLanguageController.addAction(addLanguage)

    for language in languages {
        languageChoiceButton = UIAlertAction(title: language.capitalized, style: .default, handler: { (action) -> Void in
            self.chosenLanguage = language
            self.title = self.chosenLanguage.capitalized
            print("Chosen language is: \(self.chosenLanguage)")
            self.loadInitialValues()
            chooseLanguageController.addAction(self.languageChoiceButton)
        })
    }

    let cancel = UIAlertAction(title: "Cancel", style: .cancel)  {
        (action:UIAlertAction!) in
        print("Cancel button tapped")
    }
    chooseLanguageController.addAction(cancel)

    self.navigationController!.present(chooseLanguageController, animated: true, completion: nil)
}
Tirna
  • 383
  • 1
  • 12
  • I'm not sure what you're going to achieve, but good practice would be to use either a **UIPickerView** or **UITableView**, instead of alert actions. That being said, you can design your custom view controller and show it as a _popup_ – Alchi Mar 19 '19 at 23:28
  • @Al___ Yeah I'd been considering that, so thanks for nudging me in that direction! – Tirna Mar 19 '19 at 23:35

1 Answers1

0

Try to make an array while using certain languages and make a loop afterwards to add each of them to the alerts.

@objc func languageMenu(){

    loadLanguages()

    let chooseLanguageController = UIAlertController(title: "Vocabulary Tutor", message: "Choose a Language", preferredStyle: .actionSheet)

    let i = languages.count - 1

    for n in 0...i{
        chooseLanguageController.addAction(UIAlertAction(title:  arrayLanguage[n].language, style: .default, handler: { (action) in
                print(self. languages[n])
            }))
    }

    self.present(chooseLanguageController, animated: true, completion: nil)

}

Camilo
  • 11
  • 2