UIPickerview on select item load contents UIPickerview2

Hey All,

I have an UIPickerview which i fill from a JSON file (on my webserver). When an item is selected i need to fill an second UIPIckerview from a JSON File (on my webserver)

How can i initiate the population of my second UIPickerView after the first one gets populated? (I do not use storyboards)


class AddIntController: UIViewController, UIPickerViewDataSource, UIPickerViewDelegate{

    var data: [AnyObject] = []
    var picker = UIPickerView()
    var data2: [AnyObject] = []
    var picker2 = UIPickerView()

    lazy var scrollView: UIScrollView = {
        let view = UIScrollView()
        view.translatesAutoresizingMaskIntoConstraints = false
        view.contentSize.height = 2000
        view.backgroundColor = UIColor.white
       
        return view
    }()
   
   
    let mainMenu: UIButton = {
        let image = UIImage(named: "menu.PNG")
        let l = UIButton(type: .system)
        l.addTarget(self ,action: #selector(openMenu), for: .touchUpInside)
        l.setBackgroundImage(image, for: .normal)
       
       
        return l
       
    }()
   
   
    let profileView:  UIImageView = {
        let p = UIImageView()
        p.image = UIImage(named: "default.png")
       
        p.layer.borderWidth = 14.0
        p.clipsToBounds = true
        p.layer.borderColor = UIColor.white.cgColor
       
        return p
       
    }()
   
   
    let memberTable: UITableView = {
       
        let m = UITableView()
       
        m.backgroundColor = orangeBanner
        m.separatorStyle = .none
        return m
       
    }()
   
    let memberInfoTable: UITableView = {
       
        let m = UITableView()
       
        m.backgroundColor = UIColor.white
        m.separatorStyle = .none
        return m
       
    }()
   
    let cell1: UITableViewCell = {
        let m = UITableViewCell()
        m.backgroundColor = UIColor.white
       
        return m
    }()
   
    let cell2: UITableViewCell = {
        let m = UITableViewCell()
        m.backgroundColor = grayBanner
       
        return m
    }()
   
   
    let mijnProfielField: UILabel = {
       
        let t = UILabel()
        t.text = "Mijn profiel"
        t.font = UIFont.boldSystemFont(ofSize: 18.0)
       
        t.textColor = UIColor.white
        return t
       
    }()
   
    let interesseButton: UIButton = {
       
        let h = UIButton()
        h.setTitle("   Interesses   ", for: .normal)
        h.setTitleColor(UIColor.gray, for: .normal)
        h.contentHorizontalAlignment = UIControl.ContentHorizontalAlignment.left
        h.titleLabel?.font = h.titleLabel?.font.withSize(16)
        h.addTarget(self ,action: #selector(openInteresses), for: .touchUpInside)
        return h
       
    }()
    let compButton: UIButton = {
       
        let h = UIButton()
        h.setTitle("Competenties", for: .normal)
        h.setTitleColor(UIColor.gray, for: .normal)
        h.contentHorizontalAlignment = UIControl.ContentHorizontalAlignment.left
        h.titleLabel?.font = h.titleLabel?.font.withSize(16)
        h.addTarget(self ,action: #selector(openCompetences), for: .touchUpInside)
       
        return h
       
    }()
    let beoorButton: UIButton = {
       
        let h = UIButton()
        h.setTitle("Beoordelingen", for: .normal)
        h.setTitleColor(UIColor.gray, for: .normal)
        h.contentHorizontalAlignment = UIControl.ContentHorizontalAlignment.left
        h.addTarget(self ,action: #selector(openBeoordeling), for: .touchUpInside)
        h.titleLabel?.font = h.titleLabel?.font.withSize(16)
       
        return h
       
    }()
   
    let underMember=UIView()
   
    let parentView : UITextField = {
        let textView = UITextField()
        textView.translatesAutoresizingMaskIntoConstraints  = false
        textView.text = "Groep"
        textView.backgroundColor = grayBanner
        textView.textColor = UIColor.black
        textView.heightAnchor.constraint(equalToConstant: 50.0).isActive = true
        textView.addTarget(self, action: "textFieldDidChange:", for: UIControl.Event.editingChanged)


        return textView
       
    }()
   
    let parentView2 : UITextField = {
        let textView = UITextField()
        textView.translatesAutoresizingMaskIntoConstraints  = false
        textView.text = "Rubriek"
        textView.backgroundColor = grayBanner
        textView.textColor = UIColor.black
        textView.heightAnchor.constraint(equalToConstant: 50.0).isActive = true
        return textView
       
    }()
   
   
   
   
    override func viewDidLoad() {
        super.viewDidLoad()
        setupScrollView()
        view.backgroundColor = .white
       
        picker.delegate = self
        picker.dataSource = self
        picker2.delegate = self
        picker2.dataSource = self
       
        parentView.inputView = picker
        parentView2.inputView = picker2
        //get the values from sql/Json
        let url = NSURL(string: "https://gipaa.nl/com_json.php")
       
        let data = NSData(contentsOf: url! as URL)
        var tmpValues = try! JSONSerialization.jsonObject(with: data! as Data, options: JSONSerialization.ReadingOptions.mutableContainers) as! NSArray
        tmpValues = tmpValues.reversed() as NSArray
        reloadInputViews()
       
       
        for candidate in tmpValues {
            if let cdict = candidate as? NSDictionary {
               
                //fullName is the column name in sql/json
                let names = cdict["name"]
                self.data.append(names! as AnyObject)
               
            }
        }


       
    }
   
   
        private func setupScrollView(){
            view.addSubview(scrollView)
           
           
            scrollView.topAnchor.constraint(equalTo: view.topAnchor).isActive = true
            scrollView.bottomAnchor.constraint(equalTo: view.bottomAnchor).isActive = true
            scrollView.leftAnchor.constraint(equalTo: view.leftAnchor).isActive = true
            scrollView.rightAnchor.constraint(equalTo: view.rightAnchor).isActive = true
            scrollView.keyboardDismissMode = .onDrag
            scrollView.isScrollEnabled = true
           
            // Do any additional setup after loading the view
           
            let imageView = UIImageView(frame: CGRect(x: 10, y: 5, width: 150, height: 50))
            imageView.image = UIImage(named: "main.PNG")
           
            scrollView.addSubview(imageView)
           
            scrollView.addSubview(mainMenu)
           
            mainMenu.translatesAutoresizingMaskIntoConstraints = false
            mainMenu.topAnchor.constraint(equalTo: scrollView.topAnchor, constant: 5).isActive = true
           
           
            mainMenu.rightAnchor.constraint(equalTo: view.rightAnchor, constant: -16).isActive = true
            mainMenu.heightAnchor.constraint(equalToConstant: 50).isActive = true
            mainMenu.widthAnchor.constraint(equalToConstant: 50).isActive = true
           


            view.addSubview(memberTable)
           
            memberTable.translatesAutoresizingMaskIntoConstraints = false
           
            memberTable.topAnchor.constraint(equalTo: mainMenu.bottomAnchor, constant: 10).isActive = true
            memberTable.leftAnchor.constraint(equalTo: view.leftAnchor, constant: 0).isActive = true
            memberTable.rightAnchor.constraint(equalTo: view.rightAnchor, constant: 0).isActive = true
            memberTable.heightAnchor.constraint(equalToConstant: 50).isActive = true
           
           
            view.addSubview(mijnProfielField)
           
            mijnProfielField.translatesAutoresizingMaskIntoConstraints = false
           
            mijnProfielField.topAnchor.constraint(equalTo: mainMenu.bottomAnchor, constant: 5).isActive = true
           
            mijnProfielField.rightAnchor.constraint(equalTo: view.rightAnchor, constant: -10).isActive = true
            mijnProfielField.widthAnchor.constraint(equalToConstant: 120).isActive = true
            mijnProfielField.heightAnchor.constraint(equalToConstant: 60).isActive = true
           
           
            view.addSubview(interesseButton)
           
            interesseButton.translatesAutoresizingMaskIntoConstraints = false
           
            interesseButton.topAnchor.constraint(equalTo: mijnProfielField.bottomAnchor, constant: 10).isActive = true
            interesseButton.leftAnchor.constraint(equalTo: view.leftAnchor, constant: 10).isActive = true
           
            interesseButton.heightAnchor.constraint(equalToConstant: 30).isActive = true
           
            view.addSubview(compButton)
           
            compButton.translatesAutoresizingMaskIntoConstraints = false
           
            compButton.topAnchor.constraint(equalTo: mijnProfielField.bottomAnchor, constant: 10).isActive = true
            compButton.centerXAnchor.constraint(equalTo: view.centerXAnchor, constant: -10).isActive = true
           
            compButton.heightAnchor.constraint(equalToConstant: 30).isActive = true
           
           
            view.addSubview(beoorButton)
           
            beoorButton.translatesAutoresizingMaskIntoConstraints = false
           
            beoorButton.topAnchor.constraint(equalTo: mijnProfielField.bottomAnchor, constant: 10).isActive = true
            beoorButton.rightAnchor.constraint(equalTo: view.rightAnchor, constant: -10).isActive = true
           
            beoorButton.heightAnchor.constraint(equalToConstant: 30).isActive = true
           
            view.addSubview(underMember)
           
            underMember.translatesAutoresizingMaskIntoConstraints = false
           
            underMember.topAnchor.constraint(equalTo: beoorButton.bottomAnchor, constant: 15).isActive = true
            underMember.leftAnchor.constraint(equalTo: view.leftAnchor, constant: 16).isActive = true
            underMember.rightAnchor.constraint(equalTo: view.rightAnchor, constant: -16).isActive = true
            underMember.heightAnchor.constraint(equalToConstant: 2).isActive = true
           
            underMember.backgroundColor=grayBanner
           
            // Add rounded corners to UIView
            underMember.layer.cornerRadius=0
           
            // Add border to UIView
           
            view.addSubview(parentView)
           
            parentView.translatesAutoresizingMaskIntoConstraints = false
           
            parentView.topAnchor.constraint(equalTo: underMember.bottomAnchor, constant: 15).isActive = true
            parentView.leftAnchor.constraint(equalTo: view.leftAnchor, constant: 16).isActive = true
            parentView.rightAnchor.constraint(equalTo: view.rightAnchor, constant: -16).isActive = true
          
           
            view.addSubview(parentView2)
           
            parentView2.translatesAutoresizingMaskIntoConstraints = false
           
            parentView2.topAnchor.constraint(equalTo: parentView.bottomAnchor, constant: 15).isActive = true
            parentView2.rightAnchor.constraint(equalTo: view.rightAnchor, constant: -16).isActive = true
            parentView2.leftAnchor.constraint(equalTo: view.leftAnchor, constant: 16).isActive = true
          
           


    }
   
   
   
    @objc func openBeoordeling(){
       
        let beoordeling = BeoordelingController()
        present(beoordeling, animated: true, completion: nil)
    }


    @objc func openInteresses(){
       
        let interesses = InteressesController()
        present(interesses, animated: true, completion: nil)
    }
   
    @objc func openCompetences(){
       
        let comp = CompetencesController()
        present(comp, animated: true, completion: nil)
    }
    @objc func openMenu(){
       
        let menu = MenuController()
        present(menu, animated: true, completion: nil)
    }
   
   
   
    func numberOfComponents(in pickerView: UIPickerView) -> Int {
        return 1
    }
   
    func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
      
        if pickerView == picker {
           
        return data.count
           
        }else{
           
        return data2.count
           
        }
    }
   
    func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
       
        if pickerView == picker {
        parentView.text = data[row] as! String
           
        }else {
        parentView2.text = data2[row] as! String
        }
       
        }
   
   
    func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String! {
        if pickerView == picker {
        return data[row] as! String
        }else{
        return data2[row] as! String
        }
    }
   
    func textFieldDidChange(textField: UITextField) {
        print("test")
        print("textField: \(parentView.text!)")
    }
       
   
   
}
Answered by Claude31 in 353648022

So, you will do this loading in


    func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) { 
        
        if pickerView == picker { 
          parentView.text = data[row] as! String 
// Load here picker 2 datasource
        } else { 
          parentView2.text = data2[row] as! String 
        } 
        
      }


What is it exactly you don't know how to do ?

Accepted Answer

So, you will do this loading in


    func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) { 
        
        if pickerView == picker { 
          parentView.text = data[row] as! String 
// Load here picker 2 datasource
        } else { 
          parentView2.text = data2[row] as! String 
        } 
        
      }


What is it exactly you don't know how to do ?

Thank you! very stupid 😟
I put the function here:

  1. func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String! {
  2. if pickerView == picker {
  3. return data[row] as! String
  4. // i put it here
  5. }else{
  6. return data2[row] as! String
  7. }
  8. }


When that didnt work i thought maybe i can do something with a textFieldDidChange function but that only works if change the content by typing.


Got it working now. Thanks

UIPickerview on select item load contents UIPickerview2
 
 
Q