0

I have a PickerView which has different office choices and I want to reach this office references as Strings in another ViewController. My PickerViewController class :

import UIKit
class PickerViewController: UIViewController,UIPickerViewDataSource,UIPickerViewDelegate {
  @IBOutlet weak var officePicker: UIPickerView!

  let offices = ["Choice 1","Choice 2","Choice 3"]
  override func viewDidLoad() {
    super.viewDidLoad()
    officePicker.dataSource=self
    officePicker.delegate=self
  }

  func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int {
    return 1
  }
  func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
    return offices.count
  }

  func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
    let value = offices[row]
  }
}

I have a home screen which has office informations. I want to add office info which selected before with using PickerView to this screen from a database with comparing the office names.

3
  • @Umair Afzal I tried but had same error as "Value type of NSObject has no member 'storyboard'. " Commented Jun 30, 2016 at 7:23
  • so you should be posting that as question Commented Jun 30, 2016 at 7:28
  • @Umair Afzal I posted this for your self-aggrandizement. Commented Jun 30, 2016 at 7:32

1 Answer 1

1

You can store your selected picker in a variable String. First declare it.

var selectedOffice: String?

In your pickerView delegate, just assign that variable.

func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int)
   {
      let value=offices[row]
      selectedOffice = value
   }
}

When you're ready to go to next view controller, the simplest way is to pass the data with that viewcontroller. Before that, just create another variable in the viewcontroller that you want.

var selectedOffice: String?

In your current view controller and when you want to go to next controller, assign the variable as shown below as example.

let storyboard = UIStoryboard(name: "Main", bundle: nil)
let vc = self.storyboard!.instantiateViewControllerWithIdentifier("frontView") as! FrontViewController
vc.selectedOffice = selectedOffice
self.navigationController?.pushViewController(vc, animated: true)
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you so much for your suggestions. I have an error "Value type of NSObject has no member 'storyboard'. " @Syed M
Thanks you so much ! @Syed M

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.