2017-01-10 2 views
1

私は単純なカウントダウンタイマー(Swift)に取り組んでいます。時間が「0」に達すると、alertViewを表示したい。そのためにJSSAlertViewポッドを使用しています。<JSSAlertView.JSSAlertViewを提示しようとしています...既に提示中です

すべてがうまく動作しますが、そのalertViewと私もこれを取得しています:警告:試みがどの上に存在するが、すでに私はそれを修正するにはどうすればよい

を提示していますか?

私はStoryboardまたはXibファイルを使用しません。すべてがプログラムで書かれていました。
Googleを使用してさまざまなソリューションを試しましたが、何も私のために働いていませんでした。

P.S. 以下に自分のコードを添付しました。私は2つのViewControllersを持っている:

ファーストのViewControllerは、スタートボタンを持っている:

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

乾杯:

class FirstViewController: UIViewController { 

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

第二のViewControllerは、タイマー機能やalertViewを持っています!

+0

JSAlertviewではわかりませんが、http://stackoverflow.com/questions/36450931/countdown-in-uialertcontroller助けてもらえます –

+0

Mike Alterありがとうございますが、少し違っています...私の友人は私を助けてくれましたその問題を解決する。 – Jonagold

答えて

0

解決済み。

は私が理由としてすぐに私は常にalertView呼ばれ始めた「== 0秒」として、そのエラーを得ていた。

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とfalseに割り当てられました。秒== 0とsecondsLeft == falseの場合、私がチェックしてるalertViewを呼び出すために前に

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が表示され、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!

任意のアイデアを取得していますか?

関連する問題