根据其他下拉列表选择项目更改dropdwon选项(swift 3)

时间:2017-02-05 08:13:35

标签: swift xcode swift3 dropdown

我很难找到解决这个问题的简单方法。

我有这段代码

@IBAction func agefromtapped(_ sender: UIButton) {
    agefromdrop.show()
}
func setupAgefromDropDown() {
    agefromdrop.anchorView = ageFrombtn
    agefromdrop.bottomOffset = CGPoint(x: 0, y: ageFrombtn.bounds.height)
    // You can also use localizationKeysDataSource instead. Check the docs.
    agefromdrop.dataSource = [
        "20",
        "21",
        "22",
        "23",
        "24"
    ]
    // Action triggered on selection
    agefromdrop.selectionAction = { [unowned self] (index, item) in
        self.ageFrombtn.setTitle(item, for: .normal)
    }
}

@IBAction func agetotapped(_ sender: UIButton) {
    agetodrop.show()
}
func setupAgeToDropDown() {
    agetodrop.anchorView = ageTobtin
    agetodrop.bottomOffset = CGPoint(x: 0, y: ageTobtin.bounds.height)
    // You can also use localizationKeysDataSource instead. Check the docs.
    agetodrop.dataSource = [
        "25",
        "26",
        "27",
        "28",
        "29",
        "30",
    ]
    // Action triggered on selection
    agetodrop.selectionAction = { [unowned self] (index, item) in
        self.ageTobtin.setTitle(item, for: .normal)
    }
}

我要做的是当用户从agefromdrop中选择某个年龄时,我希望agetodrop自动显示所选年份的年龄增加5年。示例:如果用户选择20,则第二个下拉列表应显示25,26,27 ...

我想出的唯一方法是每个from年龄的if语句,但这将是一个漫长的过程。

this is the dropdown Library I'm using

1 个答案:

答案 0 :(得分:1)

我认为这应该是相当简单的。

免责声明:我将对您希望如何完成此操作做一些假设。如果不是这样,您应该能够根据项目进行调整。

  1. 您在任何给定时间只需要agetodrop数据源中的六个对象
  2. selectionAction闭包中的item是一个字符串
  3. 如此简单,在闭包内响应您对agefromdrop的选择我会这样做:

    agefromdrop.selectionAction = { [unowned self] (index, item) in
        self.ageFrombtn.setTitle(item, for: .normal)
        let baseNumber = Int(item)
        self.agetodrop.dataSource = [String]()
        for i in 0 ..< 6 { //you can alter this line for however many values you want
            self.agetodrop.dataSource.append(String(baseNumber + 5 + i))
        }
    }
    

    我对这个库并不熟悉,所以如果你需要告诉agetodrop菜单在更改内容后需要更新它,那么在闭包内也要这样做。

    如果我能做些什么可以让这个答案更适合您的使用,请告诉我,但我希望它有用。