我有一个按钮,当我的计时器以Xcode结尾时,我想禁用它,但是要禁用该按钮,它必须是一个插座。但是,我需要将它作为一个动作,因为它是一个重要的按钮。
代码:
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.
}
}
答案 0 :(得分:1)
我认为您在 IBOutlet 和 IBAction 之间有些混淆。
实际上这是区别:
”“您需要为方法指定 IBAction 用于对象的Interface Builder和 IBOutlet 界面生成器”
从buttonOneOutlet
( IBOutlet )和buttonOne
( IBAction )开始,我对您的代码做了一些更改。
我添加了名为buttonOne
的 IBOutlet ,并重命名了您的 IBActions ,例如buttonOneTouched
和startButtonTouched
。 Here您可以在 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.
}
}
答案 1 :(得分:0)
您可以按照与处理这些标签相同的方式进行操作
在ViewController中定义按钮。
@IBOutlet weak var buttonOneOutlet: UIButton!
通过控制拖动将情节提要中的IBOutlet设置为该按钮。
希望有帮助
答案 2 :(得分:0)
这是您可以遵循的步骤-
对于@IBOutlet
1。在已经为UIlabel声明的类名之后,声明“界面”构建器的Element属性
class ViewController: UIViewController{
@IBOutlet weak var btnTest : UIButton! //Your Interface builder Element
对于@IBAction
1。在类中编写一个方法(例如SomeViewController)
@IBAction func didTapTestBtn(_sender : AnyObject){
//write your code here.
}
2。从Storyboard上获取方法。