首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Swift // NSPopUpButton .addingItems()问题

Swift // NSPopUpButton .addingItems()问题
EN

Stack Overflow用户
提问于 2018-11-28 00:14:43
回答 1查看 174关注 0票数 0

我找了很多但什么也没找到..。我对编程世界比较陌生,我正在用Xcode编写一个小的macOS软件。但问题是:当在弹出的n°1中选择指定的项时,我想在清除菜单后将项添加到弹出菜单n°2 (NSPopUpButton)中。因此,我将这两个项都做了一个出口,并编写了if语句,但它不起作用。在应用程序中,当选择第一个指定的项时(因为在应用程序启动时已经选择了第一个项),弹出菜单n°2将显示我想要的内容,但是如果我将项目更改为"item 2“(或者在我的例子中是”不常见的“而不是”公共的“),什么都不会发生。在应用程序启动时显示选定的“公共”图像 // 显示“不寻常”的图像,所选结果与以前相同 --我认为这是因为我的第一个'if‘语句的主体被执行了,所以其他语句没有执行,但是我不知道如何修复它。我尝试使用'switch‘而不是'if’或' if/else‘语句,但它不起作用。我也试着对每一个if语句做一个函数,但是没有发生任何事情。我希望你能帮我。谢谢大家。

代码语言:javascript
复制
    @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"]
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-11-28 10:39:03

好的,我解决了这个问题,它就像把'if‘语句放在一个"@IBAction (_发送方: NSPopUpButton)“的正文中一样简单。昨天时间很晚,我太累了,想不出用这种方法解决问题。下面是那些遇到相同问题的人的修改代码:

代码语言:javascript
复制
    @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)
    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/53510206

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档