简体   繁体   English

用户使用swift单击表格视图中的单元格之一后,如何运行计时器?

[英]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))
         }

     }
  1. I don't see any call to runTimer . 我看不到任何对runTimer调用。 So the timer never starts. 因此计时器永远不会启动。 I suggest using viewWillAppear or viewDidAppear . 我建议使用viewWillAppearviewDidAppear Add this to PopupViewController: 将此添加到PopupViewController:

     override func viewWillAppear(_ animated: Bool) { super.viewWillAppear(animated) runTimer() } 
  2. 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)
    }
  1. You have used the wrong quotes (” instead of "), hence the error. 您使用了错误的引号(“而不是”),因此出现了错误。

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.

相关问题 如何在表格视图中删除空单元格? iOS Swift - How do I remove empty cells in table view? iOS Swift 如何在Swift 3中随机化表格视图单元格? - How do I randomize table view cells in Swift 3? 如何将多个选定的表视图单元格数据传输到Swift中的下一个视图控制器? - How do I transfer multiple selected table view cells data to the next view controller in Swift? 如何在表格视图中居中放置单元格? - How do I center cells in table view? 如何获取 Swift 中表格视图单元格数据的总和? - How can I take the sum of table view cells' data in Swift? 如何快速订购两个动态表格视图单元格? - How can I order two dynamic table view cells in swift? 如何在表视图下添加视图,但它会滚动显示单元格? - How do I add a view underneath a Table View, but it scrolls with the cells? 如果我知道用户最有可能很快选择视图控制器,该如何提前加载它? - How do I load a view controller in advance if I know the user will most likely select it soon? 如何使用来自另一个视图控制器的用户输入来使用表视图单元格创建列表 - How to make a list with Table View Cells with user input from another view controller swift 当用户单击单元格时,如何从表详细视图中的数组加载视频? - How do you load video from an array in table detail view when the user clicks on the cell?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM