【发布时间】:2018-04-16 11:28:37
【问题描述】:
我尝试关注这个使用字符串值的guide,但它并没有那么有用。我正在开发一个有 8 个按钮的应用程序,每个按钮都会分配一个数字,表示在 CountdownVC 中转换为分钟的秒数。当我点击一个按钮并将值分配给 CountdownVC 中的 var seconds = 0 时,我试图传递 Int 值。
按下 MainVC 中的按钮时:
@IBAction func oneMinuteBtnPressed(_ sender: Any) {
let vc = CountdownVC()
vc.seconds = 60
performSegue(withIdentifier: "toCountdownVC", sender: self)
}
我想将分配的值传递给 CountdownVC,使其成为var seconds = 60:
import UIKit
class CountdownVC: UIViewController {
@IBOutlet weak var countdownLbl: UILabel!
@IBOutlet weak var startBtn: UIButton!
@IBOutlet weak var pauseBtn: UIButton!
@IBOutlet weak var resetBtn: UIButton!
// Variables
var seconds = 0 // This variable will hold the starting value of seconds. It could be any amount over 0
var timer = Timer()
var isTimerRunning = false // This will be used to make sure the only one time is created at a time.
var resumeTapped = false
override func viewDidLoad() {
super.viewDidLoad()
pauseBtn.isEnabled = false
}
func runTimer() {
timer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: (#selector(CountdownVC.updateTimer)), userInfo: nil, repeats: true)
isTimerRunning = true
pauseBtn.isEnabled = true
}
@objc func updateTimer(){
if seconds < 1 {
timer.invalidate()
//TODO: Send alert to indicate "time is up!"
} else {
seconds -= 1 //This will decrement(count down) the seconds.
countdownLbl.text = timeString(time: TimeInterval(seconds)) //This will update the label
}
}
func timeString(time: TimeInterval) -> String {
let hours = Int(time) / 3600
let minutes = Int(time) / 60 % 60
let seconds = Int(time) % 60
return String(format: "%02i:%02i:%02i", hours, minutes, seconds)
}
除了 segue 部分之外,我上面的内容似乎不起作用。任何帮助将不胜感激。
【问题讨论】:
-
因为你的
let vc = CountdownVC()和CountdownVC不同,所以你performSegue(withIdentifier: "toCountdownVC"。