Swift // NSPopUpButton .addingItems () проблемы - PullRequest
0 голосов
/ 28 ноября 2018

Я много искал, но ничего не нашел ... Я относительно новичок в мире программирования и пишу небольшое программное обеспечение macOS для Xcode.Но есть проблема: я хочу добавить элементы во всплывающее меню № 2 (NSPopUpButton) после очистки меню, когда во всплывающем окне № 1 выбран определенный элемент.Таким образом, я сделал вывод обоим и написал заявления if, но это не работает.В приложении, когда выбран первый указанный элемент (поскольку первый элемент уже выбран при запуске приложения), всплывающее меню № 2 показывает, что я хочу, но если я изменю элемент на «элемент 2»(или в моем случае «Uncommon» вместо «Common») ничего не происходит. изображение, показывающее «Общий», выбранный при запуске приложения // изображение, показывающее «Необычный» выбранный с теми же результатами, что и раньше Я думаю, что это происходит, потому что тело моего первого выражения «если»выполняется, так что другие нет, но я не знаю, как это исправить.Я пытался использовать «switch» вместо «if» или «if / else if / else», но это не сработало.Я также пытался сделать функцию каждого оператора if, но тогда ничего не произошло.Я надеюсь, что вы могли бы помочь мне.Спасибо всем.

    @IBOutlet weak var ingredientRarityNorthlands: NSPopUpButton!
    @IBOutlet weak var ingredientListNorthlands: NSPopUpButton!


   override func viewDidLoad() {
    super.viewDidLoad()


    if ingredientRarityNorthlands.titleOfSelectedItem == "Common" {
        ingredientListNorthlands.removeAllItems()
        ingredientListNorthlands.addItems(withTitles: ingredientListNorthlandsCommon)
    }
    if ingredientRarityNorthlands.titleOfSelectedItem == "Uncommon" {
        ingredientListNorthlands.removeAllItems()
        ingredientListNorthlands.addItems(withTitles: ingredientListNorthlandsUncommon)
    }

    if ingredientRarityNorthlands.titleOfSelectedItem == "Rare" {
        ingredientListNorthlands.removeAllItems()
        ingredientListNorthlands.addItems(withTitles: ingredientListNorthlandsRare)
    }
}

let ingredientListNorthlandsCommon = ["ok", "ko"]
let ingredientListNorthlandsUncommon = ["grer", "egr"]
let ingredientListNorthlandsRare = ["ok", "okk"]

1 Ответ

0 голосов
/ 28 ноября 2018

Хорошо, я справился с проблемой, и это было так же просто, как поместить мои операторы if в тело «@IBAction func myPopUp (_ sender: NSPopUpButton)».Поздний час вчера просто заставил меня слишком устать думать, чтобы решить проблему таким способом.Вот модифицированный код для тех, кто сталкивается с той же проблемой:

    @IBAction func ingredientRarityNorthlands(_ sender: NSPopUpButton) {
    if ingredientRarityNorthlands.titleOfSelectedItem == "Common" {
        ingredientListNorthlands.removeAllItems()
        ingredientListNorthlands.addItems(withTitles: ingredientListNorthlandsCommon)
    }
    if  ingredientRarityNorthlands.titleOfSelectedItem == "Uncommon" {
        ingredientListNorthlands.removeAllItems()
        ingredientListNorthlands.addItems(withTitles: ingredientListNorthlandsUncommon)
    }
    if ingredientRarityNorthlands.titleOfSelectedItem == "Rare" {
        ingredientListNorthlands.removeAllItems()
        ingredientListNorthlands.addItems(withTitles: ingredientListNorthlandsRare)
    }
}
...