[英]How do I run a timer as soon as user clicks one of the cells in the table view using swift?
I have two view controllers in my UI and what I am aiming to do is as soon as the user clicks on one of the cells in the table view, a pop up will appear (the other view controller) with a timer 我的UI中有两个视图控制器,我的目标是一旦用户单击表视图中的一个单元格,就会弹出带有计时器的弹出窗口(另一个视图控制器)
However, when I build the app and click on one of the cells the label still says label and does not count down 但是,当我构建应用程序并单击其中一个单元格时,标签仍显示为label并且不递减计数
I am also getting an error saying Unicode curly quote found replace with "" and expected digit after integer literal prefix in the same line (I indicated which line it is below) 我也收到一条错误消息,说发现Unicode卷曲引号在同一行中用整数文字前缀后的“”和期望的数字替换(我指出了它在下面的行)
this class is for the view controller with the table view 此类用于带有表视图的视图控制器
public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int
{
return(redeem.count)
}
public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
{
//bringing down the array to print
let cell = UITableViewCell(style: UITableViewCellStyle.default, reuseIdentifier: "cell")
cell.textLabel?.text = redeem[indexPath.row]
return(cell)
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath)
{
let popOverVC = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "sbPopUpID") as! PopUpViewController
self.addChildViewController(popOverVC)
popOverVC.view.frame = self.view.frame
self.view.addSubview(popOverVC.view)
popOverVC.didMove(toParentViewController: self)
}
This class is for the PopUpViewController 此类用于PopUpViewController
import UIKit
class PopUpViewController: UIViewController
{
@IBOutlet weak var timerLabel: UILabel!
@IBAction func closePopUp(_ sender: Any)
{
self.view.removeFromSuperview()
}
var seconds = 60
var timer = Timer()
var isTimeRunning = false
override func viewDidLoad()
{
super.viewDidLoad()
self.view.backgroundColor = UIColor.black.withAlphaComponent(0.8)
}
func runTimer()
{
timer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: (#selector(PopUpViewController.updateTimer)), userInfo: nil, repeats: true)
}
func timeString(time:TimeInterval) -> String
{
let hours = Int(time) / 3600
let minutes = Int(time) / 60 % 60
let seconds = Int(time) % 60
//getting the error message for the line below
return String(format:”%02i:%02i:%02i”, hours, minutes, seconds)
}
func updateTimer()
{
if seconds < 1
{
self.view.removeFromSuperview()
}
else
{
seconds -= 1
//getting an error message here saying EXC_BAD_INSTRUCTION (unexpectedly found nil while unwrapping an Optional value)
timerLabel.text = timeString(time: TimeInterval(seconds))
}
}
I don't see any call to runTimer
. 我看不到任何对
runTimer
调用。 So the timer never starts. 因此计时器永远不会启动。 I suggest using
viewWillAppear
or viewDidAppear
. 我建议使用
viewWillAppear
或viewDidAppear
。 Add this to PopupViewController: 将此添加到PopupViewController:
override func viewWillAppear(_ animated: Bool) { super.viewWillAppear(animated) runTimer() }
You just call self.view.removeFromSuperview()
to close the popup, which removes the view from view hierarchy but the viewController is still registered as child viewController. 您只需调用
self.view.removeFromSuperview()
即可关闭弹出窗口,该弹出窗口将视图从视图层次结构中删除,但viewController仍注册为子viewController。 I suggest presenting the popup with presentViewController
and removing it with dismissViewControllerAnimated
. 我建议使用
presentViewController
呈现弹出窗口,并使用presentViewController
将其dismissViewControllerAnimated
。
Present the view controller: 呈现视图控制器:
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath)
{
let popOverVC = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "sbPopUpID") as! PopUpViewController
self.present(popOverVC, animated: true, completion: nil)
}
Dismiss it with: 使用以下命令将其关闭:
@IBAction func closePopUp(_ sender: Any)
{
self.dismiss(animated: true, completion: nil)
}
Basic code needed. 需要基本代码。 Remember, you need to make sure whenever you make a new timer to make sure to have invalidate() called on the existing one or you will spawn multiple timers where you probably don't want to.
请记住,您需要确保每当创建一个新计时器时,都要确保在现有计时器上调用invalidate(),否则将在您可能不想使用的地方产生多个计时器。
class myClass: UIViewController {
//...
var timer:Timer!
//...
func createTimer(interval: TimeInterval, selector: Selector) {
if timer == nil {
timer = Timer.scheduledTimer(timeInterval: interval, target: self, selector: selector , userInfo: nil, repeats: true)
}
}
//...
func cancelTimer() {
timer.invalidate()
timer = nil
}
//...
override func viewWillDisappear(_ animated: Bool) {
cancelTimer()
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.