gpt4 book ai didi

ios - UIButtons 数组,出现错误 : "Unexpectedly found nil while unwrapping optional value"

转载 作者:行者123 更新时间:2023-11-28 09:20:15 25 4
gpt4 key购买 nike

我尝试在子类中调用startGame() 方法,但是,nButton 似乎没有初始化。

错误是

unexpectedly found nil while unwrapping optional value.


代码:

import UIKit

class ViewController: UIViewController {

@IBOutlet var startButton: UIButton?
@IBOutlet var nButton: Array<UIButton>?
@IBOutlet var overLable: UILabel?

func indexOf(buttonToFind:UIButton,array:Array<UIButton>)->Int
{
var count = 0
for item in array
{
if(buttonToFind != item)
{
count++
}
else
{
break
}
}
return count
}

var deck = ["1","2","3","4","5","6","7","8","9","10","11","12","13","14","15","16","17","18","19","20","21","22","23","24","25",]
var next = 0

@IBAction func pressButton(sender: UIButton)
{
var buttonIndex = indexOf(sender, array: nButton!)
if(sender.titleLabel.text != deck[next])
{
overLable!.text = "Game Over"
time.invalidate()
presentViewController((self.storyboard.instantiateViewControllerWithIdentifier("container")) as UIViewController, animated: true, completion: nil)

}
else
{
overLable!.text = "Go on"
next++
}
for(var i=0; i<25; i++)
{
nButton![i].enabled=((next<25))
}

if(next==25)
{
overLable!.text = "Congratulations"
presentViewController((self.storyboard.instantiateViewControllerWithIdentifier("container")) as UIViewController, animated: true, completion: nil)
}
}

var count = 20
var time = NSTimer()

@IBAction func start(sender: UIButton)
{
startGame()
startButton!.enabled = (startButton!.titleLabel.text == "Start")
}

func update()
{
startButton!.setTitle("\(count--)", forState: UIControlState())
//timeLable2.text = "\((count--)%10)"
if(count < 0)
{
time.invalidate()
overLable!.text = "Game Over"
}
}

func startGame()
{
var number = [UInt32]()

func displayNumber(index:Int) -> UInt32
{
for( var i = 0; i<100; i++)
{
var num = 0
var inta = arc4random()%25+1
for item in number
{
if(inta == item)
{
num++
}
}
if(num==0)
{
number.append(inta)
}
}
return number[index]
}

for (var i = 0; i<25; i++ )
{
//HERE IS THE ERROR...
nButton![i].setTitle("\(displayNumber(i))", forState: UIControlState())
}

overLable!.text=""
next = 0

for(var i=0; i<25; i++)
{
nButton![i].enabled=(next==0)
}

time.invalidate()
count = 30

time = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: ("update"), userInfo: nil, repeats: true)
}
}

ContainerVC 类:

import UIKit

class ContainerViewController: ViewController
{
@IBAction func restartButton(sender: UIButton)
{
dismissViewControllerAnimated(true, completion: nil)
super.startGame()
}
}

有人知道怎么处理吗?

最佳答案

据我了解:nButton[i] 必须是匹配的 .xib 文件中 UIButton 的引用 socket 。因为您已经在棋盘游戏的界面生成器中创建了按钮。

你有没有在调试器和po nButton[i]那一行停下来? (并确保还检查 nButton 数组也不为零)?

我看不出您正在为 nButton 分配数组。喜欢self.nButton = Array<UIButton>() .

如果您在其他任何地方分配了数组,我也看不到您将所有按钮添加到 nButton 数组。


nButton 作为按钮数组的名称令人困惑 ;-)。


indexOf可能不需要方法,因为您可以简单地将每个按钮添加一个 tag (一个号码)。可以在界面构建器中轻松设置。也许那时您甚至不再需要数组。

enter image description here

然后你可以用这样的标签检查索引:

switch sender.tag {
case 1:
//index of button is one, do something
default:
//index of button is not relevant do nothing
}

关于ios - UIButtons 数组,出现错误 : "Unexpectedly found nil while unwrapping optional value",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25289637/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com