【问题标题】:Attempt to present <JSSAlertView.JSSAlertView...which is already presenting尝试呈现 <JSSAlertView.JSSAlertView...已经呈现
【发布时间】:2017-05-24 12:56:02
【问题描述】:

我正在开发简单的倒数计时器(Swift)。当时间达到“0”时,我想显示 alertView。为此,我正在使用 JSSAlertView 吊舱。

一切正常,但使用那个 alertView 我也得到了这个:警告:尝试展示已经展示的内容

我该如何解决?

我不使用 Storyboard 或 Xib 文件。一切都是以编程方式编写的。
我尝试了使用 Google 找到的不同解决方案 - 对我没有任何帮助。

附: 我在下面附上了我的代码。我有两个 ViewController:

第一个 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)
        }
      } 
    }

干杯!

【问题讨论】:

标签: ios mobile uiviewcontroller swift3 cocoapods


【解决方案1】:

解决了。

我收到了这个错误,因为一旦“秒 == 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)
        }
    }

为了修复它,我创建了全局 Bool - secondsLeft 并将其分配为 false。我将这个 Bool 放在我的代码中,如下所示:

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 之前,我正在检查 seconds == 0 和 secondsLeft == false。如果是,alertView 出现,secondsLeft 变为 - true,我不再调用 alertView。 在 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 &lt;JSSAlertView.JSSAlertView: 0x7feb1c830a00&gt; on &lt;MyApp.TimerViewController: 0x7feb1be05650&gt; whose view is not in the window hierarchy!

有什么想法吗?

【讨论】:

    猜你喜欢
    • 2015-04-21
    • 1970-01-01
    • 2016-10-20
    • 2021-11-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-07
    • 2014-10-02
    相关资源
    最近更新 更多