Invalid redeclaration of viewDidLoad - ios

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.
}

Related

Error about Picker View

I try to show "man" and "woman" but getting "signal sigabrt". Anyone please help me solve my problem
import UIKit
class ViewController: UIViewController, UIPickerViewDelegate {
#IBOutlet weak var pkvTest: UIPickerView!
var a : [String] = [String]()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
a = ["man", "woman"]
}
func numberOfComponentsInPickerView(pickerView : UIPickerView) -> Int {
return 1
}
func pickerView(pickerView : UIPickerView, numberOfRowsInComponent component : Int) -> Int {
return a.count
}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
return a[row]
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
Your first two picker view functions have the wrong names. The reason for this is that you forgot to declare your view controller as adopting UIPickerViewDataSource. As soon as you do that, the compiler will wake up to the problem and alert you to it.
Try this. Looks like you're missing the delegate and datasource in the viewdidload
class ViewController: UIViewController, UIPickerViewDelegate, UIPickerViewDatasource {
#IBOutlet weak var pkvTest: UIPickerView!
var a : [String] = [String]()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
self.pkvTest.delegate = self
self.pkvTest.datasource = self
a = ["man", "woman"]
}

UITextView throws hierarchy inconsistency

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?

How to navigate to a second viewController using a UIPickerview

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){
}
}
}

Swift: fatal error: unexpectedly found nil while unwrapping an Optional value when initializing UIlabel value

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.

unexpected build error in Xcode

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.
}
}

Resources