2017-01-10 38 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!

任何想法?

相关问题