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"
}
}
答案 0 :(得分:20)
我遇到了这个问题,结果发现我试图编辑的标签在代码运行时不存在。
结果我从父视图和子容器视图引用了相同的视图控制器。我试图更改的标签只在容器视图中,但是当两个视图都加载时,它运行了两个视图控制器,因此它试图找到父视图中不存在的标签并抛出上述错误。
所以我学到的教训......如果对视图对象的引用正在抛出NIL ..
答案 1 :(得分:3)
看起来玩家选择未初始化。
@IBOutlet var playerChoice: UILabel!
可能插座和InterfaceBuilder / Storyboard之间的连接丢失了。尝试再次连接。
我创建了一个小型演示,一切正常:
检查IBOutlet左侧的圆圈是否已填满。否则连接会丢失。
答案 2 :(得分:2)
我在Xcode 6.2中遇到了同样的问题。 我使用单独的XIB而不是故事板。对我来说问题是,使用Swift,如果名称相同,Xcode不会自动将XIB与视图控制器关联。因此,标签的IBOutlets指向零致命。
您可以将viewcontroller.xib更改为modulename.viewcontroller.xib,以便xcode可以将其与视图控制器关联,问题就会消失。
此主题提到了更多选项:
答案 3 :(得分:2)
为我解决了这个问题(每次都让我得到,特别是当你不熟悉使用故事板时)是为了确保你正在初始化你的视图控制器:
slideShowVC = (UIStoryboard(name: "Main",bundle: nil).instantiateViewControllerWithIdentifier("WWPhotoSlideShowVC") as! WWPhotoSlideShowVC)
而不是独立的xib方式:
slideShowVC = WWSlideShowVC()
否则你的所有网点都将是零,很快就会出现很多令人头痛的问题。
答案 4 :(得分:0)
我已经尝试过这段代码,它对我来说很好用:
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"
}
}
}