我有一个显示随机表情符号的标签,然后我有4个UIButtons。每一个都会显示一个随机的表情符号,其中一个将显示与标签上的表情符号相同的表情符号。它是一个匹配的游戏。
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var faceLabel: UILabel!
@IBOutlet weak var scoreLabel: UILabel!
var randomEmojiNum: UInt32 = arc4random_uniform(6) + 1
override func viewDidLoad() {
super.viewDidLoad()
faceLabel.text = genRanNum()
}
func genRanNum() -> String{
switch randomEmojiNum{
case 1: return ""
case 2: return ""
case 3: return ""
case 4: return ""
case 5: return ""
case 6: return ""
default: break
}
return "Default"
}
func correctAnswerGen() {
var correct: UInt32 = arc4random_uniform(4)
switch correct{
case 0: topLeftAnswer.titleLabel?.text = genRanNum()
case 1: topRightAnswer.titleLabel?.text = genRanNum()
case 2: bottomRightAnswer.titleLabel?.text = genRanNum()
case 3: bottomLeftAnswer.titleLabel?.text = genRanNum()
default: break
}
}
@IBOutlet weak var topLeftAnswer: UIButton!
@IBOutlet weak var topRightAnswer: UIButton!
@IBOutlet weak var bottomLeftAnswer: UIButton!
@IBOutlet weak var bottomRightAnswer: UIButton!
}
但是每次我运行它都没有按预期进行。现在我正在随机分配4个UIButton插座中的一个,设置为与标签上的表情符号相同的表情符号,但没有设置插座!
每当我跑步它看起来就像是被选中了。没有任何按钮被设置!我有预感它与设置的插座有关,我设置后编辑值。但是,如果我知道,我不会寻求帮助!哈哈欢呼!
答案 0 :(得分:2)
而不是topLeftAnswer.titleLabel?.text
只使用:
topLeftAnswer.setTitle("emoji", forState: UIControlState.Normal)
答案 1 :(得分:1)
这就是我完成这个的方法:
class ViewController: UIViewController {
@IBOutlet weak var faceLabel: UILabel!
@IBOutlet weak var scoreLabel: UILabel!
@IBOutlet weak var topLeftAnswer: UIButton!
@IBOutlet weak var topRightAnswer: UIButton!
@IBOutlet weak var bottomLeftAnswer: UIButton!
@IBOutlet weak var bottomRightAnswer: UIButton!
var randomEmojiNum: UInt32 = arc4random_uniform(6) + 1
override func viewDidLoad() {
super.viewDidLoad()
faceLabel.text = genRanNum()
correctAnswerGen()
}
func genRanNum() -> String{
switch randomEmojiNum{
case 1: return ""
case 2: return ""
case 3: return ""
case 4: return ""
case 5: return ""
case 6: return ""
default: break
}
return "Default"
}
func correctAnswerGen() {
var correct: UInt32 = arc4random_uniform(4)
switch correct{
case 0: topLeftAnswer.setTitle(genRanNum(), forState: UIControlState.Normal)
case 1: topRightAnswer.setTitle(genRanNum(), forState: UIControlState.Normal)
case 2: bottomLeftAnswer.setTitle(genRanNum(), forState: UIControlState.Normal)
case 3: bottomRightAnswer.setTitle(genRanNum(), forState: UIControlState.Normal)
default: break
}
}
}
更新:根据OP要求编辑。