I am trying to show a picker view on touching a UITextView. It works, but then it suddenly crashes, throwing the following error (Frete Empresa is the name of the project):
2016-04-13 00:36:31.837 Frete Empresa[8927:361509] *** Terminating app due to uncaught exception 'UIViewControllerHierarchyInconsistency', reason: 'child view controller:<UICompatibilityInputViewController: 0x7af38e80> should have parent view controller:<Frete_Empresa.ViewController: 0x7ae723d0> but requested parent is:<UIInputWindowController: 0x7c227200>'
The code for viewController.swift:
import UIKit
class ViewController: UIViewController, UIPickerViewDelegate, UIPickerViewDataSource {
#IBOutlet weak var txtTipoCaminhao: UITextField!
#IBOutlet weak var pickerTipoCaminhao: UIPickerView!
var arrayTipoCaminhao = ["Semileve", "Leve", "Médio", "Semipesado", "Pesado", "Extrapesado"]
#IBAction func displayPicker(sender: AnyObject) {
pickerTipoCaminhao.hidden = false
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
pickerTipoCaminhao.delegate = self
txtTipoCaminhao.inputView = pickerTipoCaminhao
pickerTipoCaminhao.hidden = true
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func numberOfComponentsInPickerView(pickerTipoCaminhao: UIPickerView) -> Int {
return 1
}
func pickerView(pickerTipoCaminhao: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return arrayTipoCaminhao.count
}
func pickerView(pickerTipoCaminhao: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
return arrayTipoCaminhao[row]
}
func pickerView(pickerTipoCaminhao: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
txtTipoCaminhao.text = arrayTipoCaminhao[row]
}
}
It appears to link the view to the name of the project, while in the code it appears to be referencing to the window control. What am I doing wrong?
Related
My code is:
import UIKit
class ViewController: UIViewController, UIPickerViewDataSource, UIPickerViewDelegate {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
#IBOutlet weak var statePicker: UIPickerView!
#IBOutlet weak var statePickerBTN: UIButton!
let states = ["Alaska,Arkansas, Alabama, California, Maine, New York"]
override func viewDidLoad() {
super.viewDidLoad()
statePicker.dataSource = self
statePicker.delegate = self
}
#IBAction func statePickerButton(_ sender: Any) {
}
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return states.count
}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
return states[row]
}
}
It says that it was "invalid redeclaration of 'viewDidLoad()'
You wrote the function
override func viewdidLoad() {
super.viewdidLoad()
}
twice in your code. Remove it. It will get resolved
I think you are new to Swift and iOS..Your error clearly shows that ""invalid redeclaration of 'viewDidLoad()'" you have declared the method viewDidLoad twice. Remove one. we cannot have multiple method with same name and arguments inside a class.
override func viewDidLoad() {
super.viewDidLoad()
statePicker.dataSource = self
statePicker.delegate = self
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
I have a picker with two options currently, and what I am looking for is, based on the selection made in the picker, to determine the next viewController below is what I tried to use however, I immediately got an error stating the last "if" statement should be converted into a let statement. Below is the code for my picker and attempt to lead into associated segue.
import UIKit
import Firebase
import CoreData
import CoreLocation
class SchoolPickerViewController: UIViewController, UIPickerViewDataSource, UIPickerViewDelegate {
#IBOutlet weak var SchoolPicker: UIPickerView!
let pickerData = ["Ohio State University", "University of Rochester"]
override func viewDidLoad() {
super.viewDidLoad()
SchoolPicker.dataSource = self
SchoolPicker.delegate = self
// Do any additional setup after loading the view.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return pickerData.count
}
func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
return pickerData[row]
}
#IBAction func schoolSelectorGoButton(sender: AnyObject) {
if pickerData = "Ohio State University" {
performSegueWithIdentifier("ohioStateSelected", sender: sender)
}
if pickerData = "University of Rochester" {
performSegueWithIdentifier("rochesterSelected", sender: sender)
}
}
}
u should get selected picker data from Array
define
var selectedIndex = 0
then in this line
func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String?
{
selectedIndex = row
return pickerData[row]
}
and in this action
#IBAction func schoolSelectorGoButton(sender: AnyObject) {
if pickerData[selectedIndex] == "Ohio State University" {
performSegueWithIdentifier("ohioStateSelected", sender: sender)
}
}
How to navigate between two viewcontrollers using a UIPickerview
I am trying to navigate between two viewcontrollers using a UIPickerview, so i first learned how to use didSelectRow to change the title of a label and then i tried using some code to navigate to another viewcontroller but that didn't work.
The code that i used was:
import UIKit
class ViewController: UIViewController, UIPickerViewDelegate, UIPickerViewDataSource {
#IBOutlet var label: UILabel!
#IBOutlet var pickerView: UIPickerView!
var food = ["hello", "hi", "hey"]
var placementAnswer = 0
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
pickerView.delegate = self
pickerView.dataSource = self
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
public func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int {
return 1
}
public func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return food.count
}
func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
return food[row]
}
func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
placementAnswer = row
}
#IBAction func labelChanged(sender: UIButton) {
if (placementAnswer == 0){
}else if (placementAnswer == 1){
let view2 =
self.storyboard?.instantiateViewControllerWithIdentifier("view2") as! ViewController2
self.navigationController?.pushViewController(view2, animated: true)
}else if (placementAnswer == 2){
}
}
}
I'm pretty new to swift (I usually code in Java and C++), and I'm building my first ios app.
In this app, I'm trying to create a picker view, and the problem is when I'm trying to set the initial value of the label point to the first item in the picker view (array) it pops up the error
"fatal error: unexpectedly found nil while unwrapping an Optional
value"
Here is my code I'm working
class ViewController: UIViewController, UIPickerViewDelegate{
#IBOutlet var usertypeLabel: UILabel!
#IBOutlet var Picker: UIPickerView!
var usertype_array = ["patient","nurse"]
//var usertype: String!
override func viewDidLoad() {
// Do any additional setup after loading the view, typically from a nib.
super.viewDidLoad()
usertypeLabel.text = usertype_array[0] //THIS IS WHERE THE ERROR OCCURS
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int{
return 1
}
func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int{
return usertype_array.count
}
func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String!{
return usertype_array[row]
}
func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int){
var rowselected = usertype_array[row]
usertypeLabel.text = rowselected
}
}
Any idea why the error happens? Note that it stills runs if I delete that line of code. Thanks.
You are missing UIPickerViewDataSource and add it here:
class ViewController: UIViewController, UIPickerViewDelegate, UIPickerViewDataSource
And in your viewDidLoad method add this:
Picker.dataSource = self
Picker.delegate = self
And here is your full working code:
import UIKit
class ViewController: UIViewController, UIPickerViewDelegate, UIPickerViewDataSource{
#IBOutlet var usertypeLabel: UILabel!
#IBOutlet var Picker: UIPickerView!
var usertype_array = ["patient","nurse"]
//var usertype: String!
override func viewDidLoad() {
// Do any additional setup after loading the view, typically from a nib.
super.viewDidLoad()
Picker.dataSource = self
Picker.delegate = self
usertypeLabel.text = usertype_array[0] //THIS IS WHERE THE ERROR OCCURS
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int{
return 1
}
func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int{
return usertype_array.count
}
func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String!{
return usertype_array[row]
}
func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int){
var rowselected = usertype_array[row]
usertypeLabel.text = rowselected
}
}
If your outlet is correct it will work fine.
Check THIS sample for more Info.
I'm attempting to build an iOS app in Xcode v6.2 using swift as a little personal project. So far I've cobbled together bits from my own research, however the following code throws an error when I try to build. Building full apps in swift is well out outside my knowledge scope so I'm hoping it's something obvious that someone can kindly guide me in the right direction.
The purpose is simply to populate a picker view, so I'm not in any way bound to the code if there is some simpler logic.
Here is the code:
import UIKit
import iAd
import QuartzCore
class ViewController: UIViewController, ADBannerViewDelegate, UIPickerViewDataSource, UIPickerViewDelegate{
#IBOutlet weak var yearPicker: UIPickerView!
#IBOutlet weak var yearLabel: UILabel!
let listView = ["2013", "2014", "2015"]
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
println("Entering super.viewDidload() function")
yearPicker.delegate = self
yearPicker.dataSource = self
}
//Data Sources
func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return listView.count
}
//Delegates
func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String! {
return listView[row]
}
func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
yearLabel.text = listView[row]
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
The error I'm receiving is:
fatal error: unexpectedly found nil while unwrapping an Optional value
and it the following line is highlighted in the IDE:
yearPicker.delegate = self
If there's anything else that might help please let me know.
So I ended up starting again, creating the connection by ctrl + dragging into the editor to create the #IBOutlet connection.
I then removed the .delegate and .dataSource methods because I received an error stating that yearPicker did not have those members. So now I'm left with this code which works perfectly :)
Thanks to Abizern for putting me on the right track.
import UIKit
import iAd
import QuartzCore
class ViewController: UIViewController, ADBannerViewDelegate, UIPickerViewDataSource, UIPickerViewDelegate{
#IBOutlet var yearPicker: [UIPickerView]!
#IBOutlet weak var yearLabel: UILabel!
let listView = ["2013", "2014", "2015"]
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
//Data Sources
func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return listView.count
}
//Delegates
func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String! {
return listView[row]
}
func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
yearLabel.text = listView[row]
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}