Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
christophe milliere
2,345 PointsHow to use 2 UIPickerView's in one View Controller with alamofire?
I have two pickview the first works because I have a string of tables is called the loading, then when I select a value in the first, second should turn with alamofire. but nothing happens and I try to utitlise to use the tag but it does not work so I do not know where I made a mistake here it desous code.
var pickerViewCategory = UIPickerView()
var pickerViewSubCategory = UIPickerView()
// array
var pickOption = ["Théâtre", "Danse", "Comédie musicale", "Opéra", "Cirque", "Exposition", "Dégustation", "Jeune public", "Concerts", "Arts de rue", "Cinéma", "Conférence", "Festival", "Visite guidée", "Atelier", "Expo-vente"]
private var _subCategoriesArray = [String]()
//viewDidLoad
override func viewDidLoad() {
super.viewDidLoad()
pickerViewCategory.delegate = self
pickerViewSubCategory.delegate = self
self.pickerViewCategory.tag = 1
}
// pickerview
func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
if self.pickerViewCategory.tag == 1 {
return pickOption.count
}else{
return _subCategoriesArray.count
}
}
func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
if self.pickerViewCategory.tag == 1 {
return pickOption[row]
}else{
return _subCategoriesArray[row]
}
}
func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
if category.tag == 3 {
category.text = pickOption[row]
let id_category = row + 1
// function call sub category with alamofire
subCategory(id_category)
category.resignFirstResponder()
// alamofire waiting for a complete reload for a second pickerview
NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(RightViewController.refreshPickerView(_:)), name: "refresh", object: nil)
}
}
// function alamofire,
func subCategory(id:Int ) {
Alamofire.request(.GET, "http://vps124843.ovh.net/api/v1/events/sub-category", parameters: ["catg_id": id]).validate().responseJSON { response in
switch response.result {
case .Success:
if let value = response.result.value {
let json = JSON(value)
dispatch_async(dispatch_get_main_queue()) {
for index in 0 ..< json["events"].count {
// add result in array _subCategoriesArray
self._subCategoriesArray.append(json["events"][index]["name"].stringValue)
}
//send notification for reload pickerview
NSNotificationCenter.defaultCenter().postNotificationName("refresh", object: nil, userInfo: nil)
}
}
case .Failure(let error):
print(error)
}
}
}
// function NSNotification for pickerview
func refreshPickerView(notification: NSNotification) {
self.pickerViewSubCategory.tag = 2
self.pickerViewSubCategory.reloadAllComponents()
}
So the first working well and that's when I select a value from the first that the second should get but nothing happens and yet so I do not know what I did wrong.
thank you in advance