9

like it says in the title, i am trying to change label text upon click of a button. Error appears at line self.playerChoice.text = "You: Rock"

import UIKit

class ViewController: UIViewController {

var player : Int = Int()

@IBOutlet weak var readyLabel: UILabel!

@IBAction func noButton(sender: AnyObject) {
    exit(0)
}

// ---------------------------------------------------------

@IBOutlet var computerChoice: UILabel!

@IBOutlet var playerChoice: UILabel!

@IBOutlet var score: UILabel!


// Variables -------------------------------------------------

let Rock : String = "Rock"
let Paper : String = "Paper"
let Scissors : String = "Scissors"

//------------------------------------------------------------



override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.

    }


// ----------------------------------------------------------------

@IBAction func rockButton(rockbut: UIButton) {

    player = 0
    var ai = arc4random_uniform(3)
    self.playerChoice.text = "You: Rock"

    }

@IBAction func paperButton(paperbut: UIButton) {

    player = 1
    var ai = arc4random_uniform(3)
    self.playerChoice.text = "You: Paper"

    }

@IBAction func scissorsButton(scissorsbut: UIButton) {

    player = 2
    var ai = arc4random_uniform(3)
    self.playerChoice.text = "You: Scissors"

        }
    }
Dharmesh Kheni
  • 71,228
  • 33
  • 160
  • 165
artie711
  • 105
  • 1
  • 2
  • 9

5 Answers5

22

I ran into this problem and it turned out that the labels that I was trying to edit didn't exist at the time the code ran.

Turns out I was referencing the same view controller from a parent view and a child container view. The labels I was trying to change were only in the container view but as both views loaded it ran the view controller for both so it tried to find the labels that didn't exist in the parent view and threw the above error.

So the lesson I learned... If a reference to a view object is throwing a NIL..

  • The View isn't properly mapped to the View Controller.
  • The object within the view isn't mapped to the referencing IBOutlet.
  • Or as in my case, there are multiple views connected to the same View Controller and references to view objects in one view are not being found in the other.
Christopher Wade Cantley
  • 7,122
  • 5
  • 35
  • 48
4

Looks like player choice is not initialized.

@IBOutlet var playerChoice: UILabel!

Maybe the connection between the outlet and InterfaceBuilder/Storyboard is lost. Try to connect it again.

I've created a small demo and everything works fine:

enter image description here

Check if the circles at the left side of your IBOutlet are filled. Otherwise the connection is lost.

Stefan
  • 5,203
  • 8
  • 27
  • 51
  • The only thing i did was control, right click label and drag to viewController. In the connections inspector, i have: Player Choice connected to view controller under referencing outlets. – artie711 Nov 24 '14 at 20:58
  • 1
    Yep, all circles are filled, I also updated the code, still doesnt work – artie711 Nov 25 '14 at 17:46
4

What fixed this for me (and it gets me everytime, especially when you are new to using storyboards) is to make sure that you are initializing your view controller like so :

slideShowVC = (UIStoryboard(name: "Main",bundle: nil).instantiateViewControllerWithIdentifier("WWPhotoSlideShowVC") as! WWPhotoSlideShowVC)

instead of the stand alone xib way :

slideShowVC = WWSlideShowVC()

Or else all your outlets will be nil na many headaches will soon follow.

shokaveli
  • 478
  • 7
  • 14
2

I ran into the same issue in Xcode 6.2. I use individual XIBs instead of storyboards. The problem for me was that with Swift, Xcode does not automatically associate the XIB with the view controller if the names are the same. Hence the IBOutlets for the labels are pointing to nil giving the fatal.

You can change the viewcontroller.xib to be called modulename.viewcontroller.xib so that xcode can associate it with the view controller and the issue goes away.

More options are mentioned on this thread:

Can't Load UIViewController XIB file in Storyboard in Swift

Community
  • 1
  • 1
peaxol
  • 497
  • 4
  • 13
0

I have tried this code and it working fine for me:

class ViewController: UIViewController {

var player : Int = Int()      //Declare this globally

@IBOutlet weak var readyLabel: UILabel!

@IBAction func noButton(sender: AnyObject) {
    exit(0)
}

// ---------------------------------------------------------

@IBOutlet var computerChoice: UILabel!

@IBOutlet var playerChoice: UILabel!

@IBOutlet var score: UILabel!


// Variables -------------------------------------------------

let Rock : String = "Rock"
let Paper : String = "Paper"
let Scissors : String = "Scissors"

//------------------------------------------------------------

@IBAction func rockButton(rockbut: UIButton) {
     player = 0
    var ai = arc4random_uniform(3)
    self.playerChoice.text = "You: Rock"
}



@IBAction func paperButton(sender: UIButton) {
     player = 1
    var ai = arc4random_uniform(3)
    self.playerChoice.text = "You: Paper"
}

@IBAction func scissorsButton(sender: UIButton) {
     player  = 2
    var ai = arc4random_uniform(3)
    self.playerChoice.text = "You: Scissors"
}



override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
    var p : String = "Undecided"

    if (player == 0) {
        var p: String = "Rock"
    } else if (player == 1) {
        var p: String = "Paper"
    } else if (player == 2) {
        var p: String = "Scissors"
    }


    }

}
Dharmesh Kheni
  • 71,228
  • 33
  • 160
  • 165
  • Yep, I even deleted it and recreated the link. Under the connections inspector for the "you" label, player choice is connected to view controller under referencing outlets – artie711 Nov 26 '14 at 12:02
  • Works fine for me, here is a link to my project, it may have something to do with the fact that i'm using multiple views... https://www.dropbox.com/sh/jtpav3tv38lafki/AAAL_N-8zNZVbKjd5ZvaNtQLa?dl=0 – artie711 Nov 26 '14 at 13:21
  • You have to use prepareForSegue function when you want to pass data from one viewController to another controller here I created a same project for you :https://www.dropbox.com/sh/nzajs15eoxwui0t/AAAIACBoVHVr-3t-bNK0Ys1za?dl=0 – Dharmesh Kheni Nov 27 '14 at 06:27
  • Thank you very much, I dont fully understand the function yet or how to properly use it but ill search online for more information. Thanks! You'll probably hear from me soon enough :p – artie711 Nov 27 '14 at 15:46
  • I have one concern though, I can access the : show Segue, only push, model or custom. Any way to fix this? – artie711 Nov 27 '14 at 17:42
  • I don't understand what you asked – Dharmesh Kheni Dec 10 '14 at 06:30
  • 1
    The prepareForSegue method doesn't work for accessing IBOutlet vars from another view controller. Regular vars do work. – AMC08 Jul 16 '15 at 18:06
  • what different thing you have done here to fix the error,@DharmeshKheni? – Satish Mavani Feb 08 '17 at 14:38