Hi to you all, I am new in iOS development and in a making of an app. I am using uitextfield connected with uipickerview in the home screen. Is there somehow a way to make it when I choose the text field the picker view to popup with the first option whitch is (lets say...) "--" whitch is unchoosable. And 2 other options whitch when clicked take me to to other view controllers.
This is my code:
import UIKit
class ViewController: UIViewController {
// Picker View
extension ViewController: UIPickerViewDelegate, UIPickerViewDataSource {
The "var identities" are the storyboard identifiers for my other view controllers (This was from a video for table views but I toth to implement them in this code here). I am asking the question for a new ideas on how to make my code doing the stuff I described at the start.
I really need help! Please!
This is my code:
import UIKit
class ViewController: UIViewController {
Code Block let options = ["--", "option 1", "option 2"] var identities = [nil, "table_vc_teacher", "table_vc"] @IBOutlet weak var optionsField: UITextField! var picker = UIPickerView() @IBAction func didTuchBtn(_ sender: Any) { let vc = storyboard?.instantiateViewController(identifier: "table_vc") as! TableViewController vc.navigationItem.largeTitleDisplayMode = .never navigationController?.pushViewController(vc, animated: true) } override func viewDidLoad() { super.viewDidLoad() picker.delegate = self picker.dataSource = self optionsField.inputView = picker optionsField.textAlignment = .center optionsField.placeholder = "Избери Програма" }
}// Picker View
extension ViewController: UIPickerViewDelegate, UIPickerViewDataSource {
Code Block func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? { return options[row] } func numberOfComponents(in pickerView: UIPickerView) -> Int { return 1 } func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int { return options.count } func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) { if (row == 0) { } optionsField.text = options[row] optionsField.resignFirstResponder() let vcName = identities[row] let viewController = storyboard?.instantiateViewController(identifier: vcName!) viewController?.navigationItem.largeTitleDisplayMode = .never self.navigationController?.pushViewController(viewController!, animated: true) }
}The "var identities" are the storyboard identifiers for my other view controllers (This was from a video for table views but I toth to implement them in this code here). I am asking the question for a new ideas on how to make my code doing the stuff I described at the start.
I really need help! Please!
I managed to help my self!
This was the code of the picker view did select row:
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
optionsField.text = options[row]
optionsField.resignFirstResponder()
let vcName = identities[row]
let viewController = storyboard?.instantiateViewController(identifier: vcName!) viewController?.navigationItem.largeTitleDisplayMode = .never self.navigationController?.pushViewController(viewController!, animated: true)}
And this is the code now:
if row == 0 {
color = UIColor.red
let alert = UIAlertController(title: "Error", message: "Please Choose Something", preferredStyle: UIAlertController.Style.alert)
And I removed the identities var from the above code.
Hope this helps someone!
This was the code of the picker view did select row:
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
optionsField.text = options[row]
optionsField.resignFirstResponder()
let vcName = identities[row]
let viewController = storyboard?.instantiateViewController(identifier: vcName!) viewController?.navigationItem.largeTitleDisplayMode = .never self.navigationController?.pushViewController(viewController!, animated: true)}
And this is the code now:
if row == 0 {
color = UIColor.red
let alert = UIAlertController(title: "Error", message: "Please Choose Something", preferredStyle: UIAlertController.Style.alert)
Code Block } else if row == 1 { let vcOne = storyboard?.instantiateViewController(identifier: "table_vc_teacher") as! TeacherViewController vcOne.navigationItem.largeTitleDisplayMode = .never self.navigationController?.pushViewController(vcOne, animated: true) } else if row == 2 { let vcTwo = storyboard?.instantiateViewController(identifier: "table_vc") as! TableViewController vcTwo.navigationItem.largeTitleDisplayMode = .never self.navigationController?.pushViewController(vcTwo, animated: true) }
And I removed the identities var from the above code.
Hope this helps someone!