gpt4 book ai didi

ios - Segue 在 if/else 子句中过早执行

转载 作者:行者123 更新时间:2023-11-29 00:51:23 25 4
gpt4 key购买 nike

以下代码

class ViewController: UIViewController {

@IBOutlet weak var knock: UIButton!
@IBOutlet weak var label: UILabel!

var count: Int = 0
var unlocked: Int = 0

override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.

}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}


@IBAction func knockKnock(sender: UIButton) {

if count < 20 {
label.text = "Knocked \(count) times."

} else if count > 19 && count < 40 {
label.text = "Knocked \(count) times."
label.textColor = UIColor.randomColor()

} else if count > 39 && count < 100 {
label.text = "That's enough."
label.textColor = UIColor.blackColor()

} else if count > 99 && count < 102 {
label.text = "I wish you hadn't done that."
label.textColor = UIColor.redColor()

} else if count == 103 {
label.text = "Fine, come in."
label.textColor = UIColor.greenColor()
count = 126
unlocked += 1

}

if unlocked > 0 {
self.performSegueWithIdentifier("InsideHomeSegue", sender:sender)
}

count += 1

}

过早地执行 Segue

 if unlocked > 0 {
self.performSegueWithIdentifier("InsideHomeSegue", sender:sender)
}

第一次按下按钮时会立即执行 segue,我不明白这一点,因为此时代码在错误 if 后面应该是无法访问的。声明,因为 int unlocked0 初始化

我也遇到过类似的问题,结果是 XCode 本身的问题,而不是我的代码的问题;我已经浏览了所有我能找到的引用 Interface Builder 中任何变量或对象的文件,检查了命名不一致、流氓导出等。

我对 Segues 的了解仍然很不稳定,并且希望任何人能够提供详细的帮助。

较大的if/else if block 一直正常工作直到最终增量,直到添加 Segue 代码。原来是self.performSegueWithIdentifier("InsideHomeSegue", sender:sender)行在 else if count == 103 { 内堵塞; var unlocked: Int添加以解决 Segue 问题无济于事。

我已经阅读了几页有关 Segues 的 XCode 文档,但没有找到任何其他我可以尝试的内容。

我可能忽略了一些非常明显的事情,但无论如何,感谢您的阅读。

干杯

编辑:我已经检查了所有的 socket ,它是当前项目中唯一的 Segue,并且没有连接到其他任何地方。 Segue 在按下按钮时激活,而不是在 View 加载时立即激活。

最佳答案

就像我的评论所说的那样,您已经将 segue 与您的按钮连接起来,因此 segue 将始终执行。它与 if 子句无关。

您应该连接 ViewController 而不是连接按钮

关于ios - Segue 在 if/else 子句中过早执行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38115105/

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