2017-01-10 40 views
1

我正在簡單的倒數計時器(斯威夫特)。當時間到達「0」時,我想顯示alertView。爲此,我使用JSSAlertView窗格。嘗試呈現<JSSAlertView.JSSAlertView ...它已經呈現

一切運作良好,但與alertView我也得到這個:警告:試圖提出在已經呈現

我怎樣才能解決這個問題?

我不使用Storyboard或Xib文件。一切都以編程方式編寫。
我v'e嘗試使用谷歌發現不同的解決方案 - 沒有爲我工作。

P.S. 我附上了我的代碼。我有兩個ViewControllers:

第一的viewController有啓動按鈕:

class FirstViewController: UIViewController { 

func startButtonCLicked(_ button: UIButton) { 
     let controller = SecondViewController() 
     present(controller, animated: true) 
    } 
} 

二的viewController具有定時功能和alertView:

class SecondViewController: UIViewController { 

func updateTimer() { 
     if seconds > 0 { 
      print(seconds) 
      seconds -= 1 

      timerLabel.text = String(Timer.timeFormatted(seconds)) 

     } else { 
      let alertview = JSSAlertView().show(self, 
               title: "Hey", 
               text: "Hey", 
               buttonText: "Hey", 
               color: UIColorFromHex(appColor.hexMainOrangeColor, alpha: 1)) 

      alertview.setTextTheme(.light) 
     } 
    } 

    override func viewDidAppear(_ animated: Bool) { 
     super.viewDidAppear(animated) 

     if timer.isValid == false { 
      timer = Timer.scheduledTimer(timeInterval: 1.0, target: self, selector: #selector(SecondViewController.updateTimer) , userInfo: nil, repeats: true) 
     } 
     } 
    } 

乾杯!

+0

不知道與JSAlertview但http://stackoverflow.com/questions/36450931/countdown-in-uialertcontroller可能會幫助 –

+0

謝謝麥克阿爾特,但它有點不同...我的朋友剛剛幫我解決這個問題。 – Jonagold

回答

0

已解決。

我得到這個錯誤,因爲只要「秒== 0」我開始不斷呼籲alertView:

func updateTimer() { 
     if seconds > 0 { 
      print(seconds) 
      seconds -= 1 
      timerLabel.text = String(Timer.timeFormatted(seconds)) 
     } else { 
      let alertview = JSSAlertView().show(self, 
               title: "Hey", 
               text: "Hey", 
               buttonText: "Hey", 
               color: UIColorFromHex(appColor.hexMainOrangeColor, alpha: 1)) 
      alertview.setTextTheme(.light) 
     } 
    } 

修復它,我創建了全球布爾 - secondsLeft並將其分配給假。我把這個布爾我的代碼裏面是這樣的:

func updateTimer() { 
     if seconds > 0 { 
      print(seconds) 
      seconds -= 1 

      timerLabel.text = String(Timer.timeFormatted(seconds)) 

     } else if seconds == 0 && !secondsLeft { 

      secondsLeft = true 
      let alertview = JSSAlertView().show(self, 
               title: "Hey", 
               text: "Hey", 
               buttonText: "Hey", 
               color: UIColorFromHex(appColor.hexMainOrangeColor, alpha: 1)) 

      alertview.setTextTheme(.light) 
      alertView.addAction(
      self.dismiss(self, animated: true, completion: nil)) 
     } 
    } 

現在纔打電話給alertView我檢查,如果秒== 0和secondsLeft ==假。如果是,則alertView顯示,secondsLeft變爲 - true,並且我不再調用alertView。 Inside viewDidAppear我再次將secondsLeft賦值爲false。

override func viewDidAppear(_ animated: Bool) { 
     super.viewDidAppear(animated) 

     secondsLeft = false 

     if timer.isValid == false { 
      timer = Timer.scheduledTimer(timeInterval: 1.0, target: self, selector: #selector(SecondViewController.updateTimer) , userInfo: nil, repeats: true) 
     } 
     } 
    } 

所以,它的工作原理......但現在我得到另一個Warning: Attempt to present <JSSAlertView.JSSAlertView: 0x7feb1c830a00> on <MyApp.TimerViewController: 0x7feb1be05650> whose view is not in the window hierarchy!

任何想法?

相關問題