UILabel returns "nil"?
UILabel returns "nil"?
我的 UIButton 有问题。例如,当我单击 UIButton Number3
并在 ViewController 上单击 checkButton
时,currentCountLabel
中的结果是 Optional(3)。单击 checkButton
后,它会为每个数字执行此操作。如果我再次单击该按钮,currentCountLabel
会显示 "nil"。这是我的所有代码:
ViewController.swift:
class ViewController: UIViewController {
var currentCountLabel: UILabel = UILabel()
var currentCount: Int = 0
var IncorrectLabel: UILabel = UILabel()
var CorrectLabel: UILabel = UILabel()
var currectAmountCorrect : Int = 0
var currentAmountIncorrect : Int = 0
var flashTimer : NSTimer = NSTimer()
var timerCounter : Int = 0
var randomImageGeneratorNumber : Int = 0
var flashingImageView: UIImageView = UIImageView()
var flashButton: UIButton = UIButton()
var currentTime = NSTimer()
func timerFunc() {
flashButton.hidden = !flashButton.hidden
flashingImageView.hidden = !flashingImageView.hidden
}
@IBAction func flashButton(sender: UIButton) {
flashButton.hidden = !flashButton.hidden
flashingImageView.hidden = !flashingImageView.hidden
flashingImageView.image = UIImage(named: "Image\(arc4random_uniform(6) + 1).png")
if (flashingImageView.image == UIImage(named: "Image1")){
randomImageGeneratorNumber == 1
}
if (flashingImageView.image == UIImage(named: "Image2")){
randomImageGeneratorNumber == 2
}
if (flashingImageView.image == UIImage(named: "Image3")){
randomImageGeneratorNumber == 3
}
if (flashingImageView.image == UIImage(named: "Image4")){
randomImageGeneratorNumber == 4
}
if (flashingImageView.image == UIImage(named: "Image5")){
randomImageGeneratorNumber == 5
}
if (flashingImageView.image == UIImage(named: "Image6")){
randomImageGeneratorNumber == 6
}
currentCount = 0
currentCountLabel.text = "\(currentCount)"
if (flashButton.hidden == true){
flashButton.hidden = true;
flashingImageView.hidden = false
}
var currentTime = NSTimer.scheduledTimerWithTimeInterval(0.5, target: self, selector: "timerFunc", userInfo: nil, repeats: false)
func timerFunc(){
self.flashingImageView.hidden = true;
flashButton.hidden = !flashButton.hidden
flashingImageView.hidden = !flashingImageView.hidden
}
}
@IBAction func checkButton(sender: UIButton) {
if (flashButton.hidden == true){
flashButton.hidden == false
flashingImageView.hidden == true
}
var currentCount = currentCountLabel.text?.toInt()
if randomImageGeneratorNumber == currentCount {
currectAmountCorrect += 1
CorrectLabel.text = "\(currectAmountCorrect)"
} else {
currentAmountIncorrect += 1
IncorrectLabel.text = "\(currentAmountIncorrect)"
}
if (currentCountLabel.text == "0"){
let alert = UIAlertView()
alert.title = "Alert"
alert.message = "You must type in an answer in order to check it"
alert.addButtonWithTitle("Understood")
alert.show()
}
currentCount == 0
currentCountLabel.text = "\(currentCount)"
}
@IBAction func clearButton(sender: UIButton) {
currentCount = 0
currentCountLabel.text = "\(currentCount)"
}
@IBAction func Number1Button(sender: UIButton) {
currentCount = currentCount * 10
currentCount = currentCount + 1
currentCountLabel.text = "\(currentCount)"
}
@IBAction func Number2Button(sender: UIButton) {
currentCount = currentCount * 10
currentCount = currentCount + 2
currentCountLabel.text = "\(currentCount)"
}
@IBAction func Number3Button(sender: UIButton) {
currentCount = currentCount * 10
currentCount = currentCount + 3
currentCountLabel.text = "\(currentCount)"
}
@IBAction func Number4Button(sender: UIButton) {
currentCount = currentCount * 10
currentCount = currentCount + 4
currentCountLabel.text = "\(currentCount)"
}
@IBAction func Number5Button(sender: UIButton) {
currentCount = currentCount * 10
currentCount = currentCount + 5
currentCountLabel.text = "\(currentCount)"
}
@IBAction func Number6Button(sender: UIButton) {
currentCount = currentCount * 10
currentCount = currentCount + 6
currentCountLabel.text = "\(currentCount)"
}
override func viewDidLoad() {
super.viewDidLoad()
var currentCountLabel = "\(currentCount)"
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}
我不知道发生了什么或如何解决它。
如有任何意见和建议,我们将不胜感激。
提前致谢。
如果您阐明了您希望它的行为方式,将会有所帮助。如果您想知道为什么它是可选的以及为什么它返回 nil
那么您可以按如下方式解决这个问题。
在您的 checkButton()
函数中,您在末尾有这一行:
currentCountLabel.text = "\(currentCount)"
将 currentCount
变量展开为:
currentCountLabel.text = "\(currentCount!)"
在同一个 checkButton()
函数中,你有这一行:
currentCount == 0
因此,如果您想知道为什么它没有返回 0
,那是因为您使用的是比较运算符而不是赋值运算符。
我的 UIButton 有问题。例如,当我单击 UIButton Number3
并在 ViewController 上单击 checkButton
时,currentCountLabel
中的结果是 Optional(3)。单击 checkButton
后,它会为每个数字执行此操作。如果我再次单击该按钮,currentCountLabel
会显示 "nil"。这是我的所有代码:
ViewController.swift:
class ViewController: UIViewController {
var currentCountLabel: UILabel = UILabel()
var currentCount: Int = 0
var IncorrectLabel: UILabel = UILabel()
var CorrectLabel: UILabel = UILabel()
var currectAmountCorrect : Int = 0
var currentAmountIncorrect : Int = 0
var flashTimer : NSTimer = NSTimer()
var timerCounter : Int = 0
var randomImageGeneratorNumber : Int = 0
var flashingImageView: UIImageView = UIImageView()
var flashButton: UIButton = UIButton()
var currentTime = NSTimer()
func timerFunc() {
flashButton.hidden = !flashButton.hidden
flashingImageView.hidden = !flashingImageView.hidden
}
@IBAction func flashButton(sender: UIButton) {
flashButton.hidden = !flashButton.hidden
flashingImageView.hidden = !flashingImageView.hidden
flashingImageView.image = UIImage(named: "Image\(arc4random_uniform(6) + 1).png")
if (flashingImageView.image == UIImage(named: "Image1")){
randomImageGeneratorNumber == 1
}
if (flashingImageView.image == UIImage(named: "Image2")){
randomImageGeneratorNumber == 2
}
if (flashingImageView.image == UIImage(named: "Image3")){
randomImageGeneratorNumber == 3
}
if (flashingImageView.image == UIImage(named: "Image4")){
randomImageGeneratorNumber == 4
}
if (flashingImageView.image == UIImage(named: "Image5")){
randomImageGeneratorNumber == 5
}
if (flashingImageView.image == UIImage(named: "Image6")){
randomImageGeneratorNumber == 6
}
currentCount = 0
currentCountLabel.text = "\(currentCount)"
if (flashButton.hidden == true){
flashButton.hidden = true;
flashingImageView.hidden = false
}
var currentTime = NSTimer.scheduledTimerWithTimeInterval(0.5, target: self, selector: "timerFunc", userInfo: nil, repeats: false)
func timerFunc(){
self.flashingImageView.hidden = true;
flashButton.hidden = !flashButton.hidden
flashingImageView.hidden = !flashingImageView.hidden
}
}
@IBAction func checkButton(sender: UIButton) {
if (flashButton.hidden == true){
flashButton.hidden == false
flashingImageView.hidden == true
}
var currentCount = currentCountLabel.text?.toInt()
if randomImageGeneratorNumber == currentCount {
currectAmountCorrect += 1
CorrectLabel.text = "\(currectAmountCorrect)"
} else {
currentAmountIncorrect += 1
IncorrectLabel.text = "\(currentAmountIncorrect)"
}
if (currentCountLabel.text == "0"){
let alert = UIAlertView()
alert.title = "Alert"
alert.message = "You must type in an answer in order to check it"
alert.addButtonWithTitle("Understood")
alert.show()
}
currentCount == 0
currentCountLabel.text = "\(currentCount)"
}
@IBAction func clearButton(sender: UIButton) {
currentCount = 0
currentCountLabel.text = "\(currentCount)"
}
@IBAction func Number1Button(sender: UIButton) {
currentCount = currentCount * 10
currentCount = currentCount + 1
currentCountLabel.text = "\(currentCount)"
}
@IBAction func Number2Button(sender: UIButton) {
currentCount = currentCount * 10
currentCount = currentCount + 2
currentCountLabel.text = "\(currentCount)"
}
@IBAction func Number3Button(sender: UIButton) {
currentCount = currentCount * 10
currentCount = currentCount + 3
currentCountLabel.text = "\(currentCount)"
}
@IBAction func Number4Button(sender: UIButton) {
currentCount = currentCount * 10
currentCount = currentCount + 4
currentCountLabel.text = "\(currentCount)"
}
@IBAction func Number5Button(sender: UIButton) {
currentCount = currentCount * 10
currentCount = currentCount + 5
currentCountLabel.text = "\(currentCount)"
}
@IBAction func Number6Button(sender: UIButton) {
currentCount = currentCount * 10
currentCount = currentCount + 6
currentCountLabel.text = "\(currentCount)"
}
override func viewDidLoad() {
super.viewDidLoad()
var currentCountLabel = "\(currentCount)"
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}
我不知道发生了什么或如何解决它。
如有任何意见和建议,我们将不胜感激。
提前致谢。
如果您阐明了您希望它的行为方式,将会有所帮助。如果您想知道为什么它是可选的以及为什么它返回 nil
那么您可以按如下方式解决这个问题。
在您的 checkButton()
函数中,您在末尾有这一行:
currentCountLabel.text = "\(currentCount)"
将 currentCount
变量展开为:
currentCountLabel.text = "\(currentCount!)"
在同一个 checkButton()
函数中,你有这一行:
currentCount == 0
因此,如果您想知道为什么它没有返回 0
,那是因为您使用的是比较运算符而不是赋值运算符。