我想創建一個簡單的應用程序,由一個導航控制器與2個屏幕由單個UIViewController類控制。該應用程序將允許用戶在文本字段中輸入0到10之間的數字,當用戶按下按鈕時,它們將被帶到第二個屏幕,顯示他們是否猜測隨機生成的數字。當我在第二個屏幕中訪問標籤的文本屬性時,出現「意外地發現零而解包可選值」錯誤。我不明白爲什麼,我把這個標籤與課堂連接起來。任何建議?發現零,而展開可選
這是我的導航控制器類代碼:
class MyNavController: UINavigationController {
var ranNum:Int = 0
override func viewDidLoad() {
super.viewDidLoad()
ranNum = (Int)(arc4random_uniform(10))
NSLog("random number: \(ranNum)")
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
這是我的通用視圖控制器類:
class GenericViewController: UIViewController {
@IBOutlet weak var inputTextField: UITextField!
@IBOutlet weak var outputLabel: UILabel!
var setThisLabel: String = "You Win!"
@IBAction func guessTheNumber(sender: AnyObject) {
var generatedRanNumber = (parentViewController as! MyNavController).ranNum
var userGuessNumer = inputTextField.text.toInt()
if generatedRanNumber == userGuessNumer {
outputLabel.text = "You Win!"
} else if generatedRanNumber < userGuessNumer {
outputLabel.text = "Think Less..."
} else {
outputLabel.text = "Think Big..."
}
}
override func viewDidLoad() {
super.viewDidLoad()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
「找到將解包可選」。誰是意志,你爲什麼在你打開可選的時候找到他? :)(我猜你的意思是「發現**零**,而解包可選。」) –
公頃,我現在看到拼錯字:D。將它改變。謝謝! :) –