skip to Main Content

Im very new to xcode.
Im getting the user to select from a UIPicker view and when they click a submit button it goes to the next screen and displays what they chose.
I got it to work with normal textboxes with segue code

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if let viewController2 = segue.destination as?
ViewController2 {
viewController2.textstr = textField.text
}
}

but i dont really know to apply the same formula with UIPickers, Segments and Steppers

3

Answers


  1. You can use this:

    class ViewController: UIViewController {
    
       var pickerValue = ""
    
       override func viewDidLoad() {
            super.viewDidLoad()
    
       }
    
       func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
    
          self.pickerValue = yourArray[row]
      }
    }
    

    And then:

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
       if segue.identifier == "ViewController2" {
            if let dvc : ViewController2 = segue.destination as? ViewController2{
     
               dcv = self.pickerValue
         }
       }
    }
    
    
      @IBAction func submitBtn(_ sender: Any) {
            
         self.performSegue(withIdentifier: "ViewController2", sender: self)
      }
    
    Login or Signup to reply.
  2. you can pass value from using thing code.

    class ViewController: UIViewController {
        
        var selectedPickerValue: String?
        
        override func viewDidLoad() {
            super.viewDidLoad()
            // Do any additional setup after loading the view.
        }
        
        override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
            
            if let viewController2 = segue.destination as? ViewController2 {
                viewController2.selectedValue = selectedPickerValue
            }
            
        }
        
        func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
            
            selectedPickerValue = //give value from you picker to this variable
            
        }
        
        
    }
    
    class ViewController2: UIViewController {
    
        var selectedValue: String?
        
        override func viewDidLoad() {
            super.viewDidLoad()
            // Do any additional setup after loading the view.
        }
    
    }
    

    I hope you find it helpful. Best of luck

    Login or Signup to reply.
  3. Try This Once:

    This Will be picked your yourArray’s Value

    class FirstViewController: UIViewController { 
       
          var pickerValue: String = ""   
          var yourArray: [String] = ["1","2","3"] 
      
          override func viewDidLoad() {
                super.viewDidLoad()    
    
          }    
          
          func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
    
              self.pickerValue = yourArray[row]    
          } 
    }
    

    **Then You want to pass selected data first to second viewController using segue code **

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
       if segue.identifier == "SecondViewController" {
            if let nextPage : ViewController2 = segue.destination as? SecondViewController {
               nextPage.selectedPickerValue = self.pickerValue
         }
       }
    }
    
     @IBAction func submitBtn(_ sender: Any) {  
    
             self.performSegue(withIdentifier: "SecondViewController", sender: self)
    
       }
    

    SecondViewController look like it:

    class SecondViewController: UIViewController {
    
        var selectedPickerValue: String?
        
        override func viewDidLoad() {
            super.viewDidLoad()
            // Do any additional setup after loading the view.
        }
    
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search