如何将nsobject模型类调用为菜单选项?

时间:2016-11-29 16:32:31

标签: ios swift drag-and-drop

这里我使用这个库文件从uiview创建下拉选项。这是我正在使用的库Drop down menu library。但是我们需要调用`menu1.option = [1,2,3,4] 1。但是在我自己的代码中,我已经分开了json flie。

我的要求是,现在我仍在显示uipicker中的县名单。但我需要在下拉选项中显示它们。如何将json称为menu.option

我的代码:

 menu1.options = ["1","2","2","3","3","4","5"] 

 menu1.editable = false 
 menu1.delegate = self

我以前的uipicker显示国家/地区列表如下:

if textField == country {
       ctCountryPicker.showPicker(self, inputText: textField)
}

我的json代码loding:

/**
     Loads content from .json file
     */
    private func loadData() {
        let bundlePath = NSBundle(forClass: CountryPicker.self).pathForResource("SwiftCountryPicker", ofType: "bundle")

        if let path = NSBundle(path: bundlePath!)!.pathForResource("EmojiCountryCodes", ofType: "json")
        {

            do {
                let jsonData = try NSData(contentsOfFile: path, options: .DataReadingMappedIfSafe)
                let json = try NSJSONSerialization.JSONObjectWithData(jsonData, options: .AllowFragments)

                var countryCode: String?

                if let local = NSLocale.currentLocale().objectForKey(NSLocaleCountryCode) as? String {
                    countryCode = local
                }

                guard let countries = json as? NSArray else {
                    print("countries is not an array")
                    return
                }

                for subJson in countries{

                    guard let name = subJson["name"] as? String, iso = subJson["code"] as? String, emoji = subJson["emoji"] as? String else {

                        print("couldn't parse json")

                        break
                    }

                    let country = Country(name: name, iso: iso, emoji: emoji)

                    // set current country if it's the local countr y
                    if country.iso == countryCode {
                        pickedCountry = country
                    }

                    // append country
                    countryData.append(country)
                }

                countryData.sortInPlace { $1.name > $0.name }
                self.reloadAllComponents()

            } catch {
                print("error reading file")

            }
        }

    }
}

现在使用menu1.option =,如何调用我的JSON文件以显示我的下拉菜单中的选项列表?

1 个答案:

答案 0 :(得分:1)

创建一个类别文件Category_country_codes并创建两个功能。

func get_Country(arr : NSArray) -> NSArray {
        let arr : NSArray = arr.valueForKey("Country") as! NSArray
        return arr
}
func get_state(arr : NSArray , index : NSString) -> NSArray {
    let arr1 = arr;
    let namePredicate = NSPredicate(format: "Country like %@",index);

    let filteredUserData  = arr1.filteredArrayUsingPredicate(namePredicate)
    print("names = ,\(filteredUserData)");

    return filteredUserData
}

class UserInfoViewController: UIViewController

var countryname : NSArray = []
var arrayOfItems : NSArray = NSArray()
let category_country = Category_country_codes()

override func viewDidLoad() {
        super.viewDidLoad()

        let chapterPath = NSBundle.mainBundle().pathForResource("country-states", ofType: "plist")
        arrayOfItems = NSArray(contentsOfFile: chapterPath!)!
            print(arrayOfItems)
        menu1.options = self.category_country.get_Country(self.arrayOfItems) as! Array<String>
        //ctCountryPicker
        let set = NSSet(array: menu1.options)
        print(set.allObjects)
        menu1.options = NSArray(array: set.allObjects as! Array<String>) as! Array<String>

        menu1.editable = false
        menu1.delegate = self

    }

  func dropDownMenu(menu: ZHDropDownMenu!, didChoose index: Int) {
      print("\(menu) choosed at index \(index)")
      let countryname : NSString = menu1.options .objectAtIndex(index) as! NSString
      let statedata = self.category_country.get_state(self.arrayOfItems, index: countryname)
      print(statedata .valueForKey("State")) // here set your second menu state . like 
      menu2.option = statedata .valueForKey("State")
  }

在ZHDropDownMenu.swfit文件中进行了一处更改 不要忘记此

 public var options:NSArray = []

阅读我的整个答案并更新您的代码。别忘了批准它。 快乐编码。