从选择器视图选择发送API请求

时间:2018-02-04 12:55:49

标签: ios swift swift4

我是swift的新手并且创建了我的第一个应用程序。

我有一个pickerview,我从api数据中填写。它有效。

现在我想再次使用第一个pickerview的选定值调用api,并将我收到的数据填入另一个pickerview。

我得到的问题:它将相同的数据(从第一个请求)填入第二个pickerview。

以下是我使用的代码:

struct Job: Decodable{
    let beruf_name: String
    let beruf_id: String
}
struct Class: Decodable{
    let klassen_id: String
    let klassen_longname: String
}

class ViewController: UIViewController, 
UIPickerViewDelegate,UIPickerViewDataSource {


@IBOutlet weak var classPicker: UIPickerView!
@IBOutlet weak var pickerview: UIPickerView!
@IBOutlet weak var label: UILabel!

var jobs = [Job]()
var classes = [Class]()

override func viewDidLoad() {
    super.viewDidLoad()
    pickerview.delegate = self
    pickerview.dataSource = self
    classPicker.delegate = self
    classPicker.dataSource = self



    let url = URL(string: "http://home.gibm.ch/interfaces/133/berufe.php")

    URLSession.shared.dataTask(with: url!) { (data, response, err) in
        if err == nil {
            do{
                try self.jobs = JSONDecoder().decode([Job].self, from: data!)
            }catch{
                print("Parse error")
            }
            DispatchQueue.main.async {
                self.pickerview.reloadComponent(0)
            }
        }
    }.resume()
}

//pickerview methods

func numberOfComponents(in pickerView: UIPickerView) -> Int {
    return 1;
}

func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
    return jobs.count
}

func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
    return jobs[row].beruf_name
}


//delegate method

func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
    let selectedJob = jobs[row].beruf_id
    label.text = selectedJob

    let class_url = URL(string: "http://home.gibm.ch/interfaces/133/klassen.php?beruf_id="+selectedJob)

    URLSession.shared.dataTask(with: class_url!) { (data, response, err) in
        if err == nil {
            do{
                try self.classes = JSONDecoder().decode([Class].self, from: data!)
                print(self.classes)
            }catch{
                print("Parse error")
            }
            DispatchQueue.main.async {
                self.classPicker.reloadComponent(0)
            }
        }
    }.resume()
}

1 个答案:

答案 0 :(得分:0)

您已将ViewController设置为pickerViewclassPicker的数据源。这很好,但目前您只返回与pickerView相关的数据,因此classPicker会填充与pickerView相同的数据。您需要说出类似的内容:

func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
    if pickerView === self.pickerView {
        return jobs.count
    }
    else {
        return classes.count
    }

}

func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
    if pickerView === self.pickerView {
        return jobs[row].beruf_name
    }
    else {
        return classes[row].klasse_longname
    }
}

您可能希望为您的委托方法func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int)

执行相同的操作

此外,当您在选择第一项后进行API调用时,您将收到一个"解析错误"这是因为API返回的对象如下:

  

{" klasse_id":" 2898290"," klasse_name":" BBM 16-18 A"," klasse_longname&# 34;:" Berufsmatura II BBM 16-18 A"}

" Klasse的"不是" klassen"所以你需要更新你的结构:

struct Class: Decodable{
    let klasse_id: String
    let klasse_name: String
    let klasse_longname: String
}