gpt4 book ai didi

swift - 如何快速使按钮成为 IBOutlet 和 IBAction

转载 作者:行者123 更新时间:2023-11-28 05:39:29 27 4
gpt4 key购买 nike

我有一个按钮,当我的计时器在 Xcode 中结束时我想禁用它,但要禁用按钮它需要一个 socket 。但是,我需要它是一个 Action ,因为它是一个重要的按钮。

代码:

import UIKit

class ViewController: UIViewController {

var count = 0
var timer = 60
var highScore = 0

@IBOutlet weak var output: UILabel!
@IBOutlet weak var timerText: UILabel!
@IBOutlet weak var scoreText: UILabel!
@IBOutlet weak var highscoreText: UILabel!

@IBAction func buttonOne(_ sender: Any) {
count += 1
output.text = String(count)
}

@IBAction func StartButton(_ sender: Any) {
Timer.scheduledTimer(timeInterval: 1.0, target: self, selector: Selector(("updateCounting")), userInfo: nil, repeats: true)
}

@objc func updateCounting(){
if timer > 0 {
timer -= 1
timerText.text = String(timer)
} else {
buttonOneOutlet.isEnabled = false
timerText.text = "Time up!"
scoreText.text = "Score: " + String(count)
if count > highScore {
highScore = count
highscoreText.text = String(count)
} else {
highscoreText.text = String(highScore)
}
}
}

override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}


}

最佳答案

我认为您混淆了 IBOutletIBAction

其实区别在于:

"You need to specify IBAction for methods that will be used in Interface Builder and IBOutlet for objects that will be used in Interface Builder"

( Source - StackOverflow )


我稍微更改了您的代码,从 buttonOneOutlet (IBOutlet) 和 buttonOne (IBAction) 开始。

我添加了一个名为 buttonOneIBOutlet 并将您的 IBActions 重命名为 buttonOneTouchedstartButtonTouchedHere您可以在 Swift 中找到一些关于命名约定的文档。

此外,我添加了一个名为 timerObject 的变量,这样您就可以保留对正在运行的计时器的引用,并在以后对它进行 invalidate()。否则计时器将永远运行。

这是更新后的代码:

import UIKit
class ViewController: UIViewController {

var count = 0
var timer = 60
var highScore = 0
// new timer object declared here
var timerObject = Timer()

// new IBOutlet object declared here
@IBOutlet weak var buttonOne: UIButton!
@IBOutlet weak var output: UILabel!
@IBOutlet weak var timerText: UILabel!
@IBOutlet weak var scoreText: UILabel!
@IBOutlet weak var highscoreText: UILabel!

// renamed the function to 'buttonOneTouched' (it was 'buttonOne')
@IBAction func buttonOneTouched(_ sender: Any) {
count += 1
output.text = String(count)
}

// renamed the function to 'startButtonTouched' (it was 'StartButton')
@IBAction func startButtonTouched(_ sender: Any) {
timerObject = Timer.scheduledTimer(timeInterval: 1.0, target: self, selector: Selector(("updateCounting")), userInfo: nil, repeats: true)
}

@objc func updateCounting() {
if timer > 0 {
timer -= 1
timerText.text = String(timer)
} else {
// added step to invalidate the running timer loop
timerObject.invalidate()
// replaced 'buttonOneOutlet' with 'buttonOne'
buttonOne.isEnabled = false
timerText.text = "Time up!"
scoreText.text = "Score: " + String(count)
if count > highScore {
highScore = count
highscoreText.text = String(count)
} else {
highscoreText.text = String(highScore)
}
}
}

override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
}

关于swift - 如何快速使按钮成为 IBOutlet 和 IBAction,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57346472/

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