簡體   English   中英

如何彈出初始視圖控制器

[英]How to pop the initial view controller

假設我有兩個視圖控制器,視圖控制器A和視圖控制器B

class ViewControllerA: UIViewController {
     override func viewDidLoad() {
        super.viewDidLoad()
        // it runs this function every 5 seconds
         Timer.scheduledTimer(5, target: self,selector: #selector(ViewControllerA.printNumber), userInfo: nil, repeats: true)
    }

    @IBAction func callViewControllerBButtonClicked(_ sender: UIButton) {
         if let vc = self.storyboard?.instantiateViewController(withIdentifier: "ViewControllerBID") as? ViewControllerB {

                self.present(vc, animated: true, completion: nil)
        }

    }

    func printNumber() {
      print(0)
    }
}

每當有人單擊callViewControllerBButtonClicked()按鈕時,它將實例化一個新的視圖控制器ViewControllerB並將其呈現在ViewControllerA頂部。 我現在面臨的問題是即使我已經在ViewControllerB上,它仍然運行此功能

Timer.scheduledTimer(5, target: self,selector: #selector(ViewControllerA.printNumber), userInfo: nil, repeats: true)

如何彈出ViewControllerA?

試試這個代碼-

class ViewControllerA: UIViewController {
     var timer : Timer!
     override func viewDidLoad() {
        super.viewDidLoad()
        // it runs this function every 5 seconds
        timer = Timer.scheduledTimer(5, target: self,selector: #selector(ViewControllerA.printNumber), userInfo: nil, repeats: true)
    }

    @IBAction func callViewControllerBButtonClicked(_ sender: UIButton) {
         if let vc = self.storyboard?.instantiateViewController(withIdentifier: "ViewControllerBID") as? ViewControllerB {
            timer.invalidate()
            self.present(vc, animated: true, completion: nil)
        }

    }

    func printNumber() {
      print(0)
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

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