【问题标题】:Populating UIAlertController with UIAlertActions based on string array使用基于字符串数组的 UIAlertActions 填充 UIAlertController
【发布时间】:2019-03-19 22:13:13
【问题描述】:

我有一个称为语言的字符串数组,我想在 UIAlertController 中创建与数组中元素一样多的 UIAlertAction。我不知道数组会有多大,因为用户可以使用同一 UIAlertController 中的 add 选项将语言添加到数组中。

loadLanguages() 成功将 userDefaults 数据加载到语言数组中,如果有任何现有语言在首次应用加载时保存为 2 种起始语言和/或由用户在后续使用应用时添加。

添加语言选项有效,并存储在 userDfeaults (self.saveLanguages) 中,并附加到语言数组中。

但是我不确定是否为语言数组中的每种语言创建 UIAlertAction 选项。我尝试循环遍历数组来生成每个菜单项,因为语言数组有关于应该显示多少 UIAlertAction 的答案,但什么也没有出现。

经过大量搜索,我没有发现任何涉及此内容的内容,但我确信有一种优雅的方法。

仅供参考:languageChoiceButton 声明为:

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)
}

【问题讨论】:

  • 我不确定您要实现什么,但最好的做法是使用 UIPickerViewUITableView,而不是警报动作。话虽如此,您可以设计自定义视图控制器并将其显示为 popup
  • @Al___ 是的,我一直在考虑这个问题,所以感谢您朝那个方向轻推我!

标签: arrays swift uialertcontroller uialertaction


【解决方案1】:

尝试在使用某些语言时创建一个数组,然后创建一个循环以将它们中的每一个添加到警报中。

@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)

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-03-10
    • 2017-02-02
    • 1970-01-01
    • 1970-01-01
    • 2015-05-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多