ios - 如何将下拉文本值获取到 label.text

标签 ios swift drop-down-menu

我正在使用这个 ZHDropDownMenuDelegate我的下拉列表选项的库。现在我有两个下拉选项。例如,我下拉了国家,州。下面是示例代码:

Emenu1.options = ["1","2","3"]

    Emenu2.options = ["1a","2b","3c"]

现在使用这两种方法函数我可以获得我选择的下拉选项:

     func dropDownMenu(menu: ZHDropDownMenu!, didChoose index: Int) {
            print("\(menu) choosed at index \(index)")

            let country : NSString = Emenu1.options .objectAtIndex(index) as! NSString

           ECountryInputName.text = country as String



            let secondmenu : NSString = Emenu2.options .objectAtIndex(index) as! NSString

              IamInputName.text! = secondmenu as String!

            print(IamInputName.text)

}


//编辑完成后回调
    func dropDownMenu(menu: ZHDropDownMenu!, didInput text: String!) {
        // print("\(menu) input text \(text)")
    }

所以在第一个菜单中,如果我选择 1,那么在我的第二个菜单中,它会自动选择 1a

如果我在第二个菜单中选择 2a,那么在第一个菜单中 2 会自动选择。

现在我如何在不使用索引值的情况下单独选择我的选项。请帮助我!!

谢谢

最佳答案

在您的 Storyboard中为每个菜单(ZHDropDownMenu)设置标签,然后执行以下操作:

func dropDownMenu(menu: ZHDropDownMenu!, didChoose index: Int) {        
    if menu.tag == 0 {
        let country : NSString = Emenu1.options .objectAtIndex(index) as! NSString
        ECountryInputName.text = country as String
    }else if menu.tag == 1 {
        let secondmenu : NSString = Emenu2.options .objectAtIndex(index) as! NSString
        IamInputName.text! = secondmenu as String!
    }
}

并且请避免使用 !,它会让您的应用程序在某个时候崩溃。 enter image description here

关于ios - 如何将下拉文本值获取到 label.text,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41004672/

相关文章:

ios - 内部修饰 (@IBAction) 不适用于 UIButton 子类

ios - UITableViewCell 排序

ios - Swift 中的非可选类型不应该包含可选类型吗?

ios - 安装pod文件后改了工程名,没想到遇到如下问题

ios - 通用对象作为 HandyJSON 的参数

html - 多级下拉 W3.CSS

iphone - 我们可以显示不同类型的 map View 吗?

ios - 如何在 ios,swift 中使用 RSA 公钥加密和解密字符串(纯文本)

vba - 如何在不选择列出的项目的情况下使用组合框按键事件

jquery - 如何制作 ( :hover:before) pseudo elements remain in effect while drop-down menu is open?