簡體   English   中英

用戶使用swift單擊表格視圖中的單元格之一后,如何運行計時器?

[英]How do I run a timer as soon as user clicks one of the cells in the table view using swift?

我的UI中有兩個視圖控制器,我的目標是一旦用戶單擊表視圖中的一個單元格,就會彈出帶有計時器的彈出窗口(另一個視圖控制器)

但是,當我構建應用程序並單擊其中一個單元格時,標簽仍顯示為label並且不遞減計數

我也收到一條錯誤消息,說發現Unicode卷曲引號在同一行中用整數文字前綴后的“”和期望的數字替換(我指出了它在下面的行)

此類用於帶有表視圖的視圖控制器

 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)


    }   

此類用於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. 我看不到任何對runTimer調用。 因此計時器永遠不會啟動。 我建議使用viewWillAppearviewDidAppear 將此添加到PopupViewController:

     override func viewWillAppear(_ animated: Bool) { super.viewWillAppear(animated) runTimer() } 
  2. 您只需調用self.view.removeFromSuperview()即可關閉彈出窗口,該彈出窗口將視圖從視圖層次結構中刪除,但viewController仍注冊為子viewController。 我建議使用presentViewController呈現彈出窗口,並使用presentViewController將其dismissViewControllerAnimated

呈現視圖控制器:

    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)
    }

使用以下命令將其關閉:

    @IBAction func closePopUp(_ sender: Any)
    {
        self.dismiss(animated: true, completion: nil)
    }
  1. 您使用了錯誤的引號(“而不是”),因此出現了錯誤。

需要基本代碼。 請記住,您需要確保每當創建一個新計時器時,都要確保在現有計時器上調用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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM