Ios 试图呈现<;JSSAlertView.JSSAlertView…已呈现
我正在研究简单的倒计时计时器(Swift)。当时间达到“0”时,我想显示alertView。为此,我使用了JSSAlertView吊舱 一切都很好,但是有了这个alertView,我也得到了这样一个提示:警告:尝试在已经呈现的内容上进行呈现 我怎样才能修好它 我不使用故事板或Xib文件。所有内容都是以编程方式编写的。Ios 试图呈现<;JSSAlertView.JSSAlertView…已呈现,ios,mobile,uiviewcontroller,swift3,cocoapods,Ios,Mobile,Uiviewcontroller,Swift3,Cocoapods,我正在研究简单的倒计时计时器(Swift)。当时间达到“0”时,我想显示alertView。为此,我使用了JSSAlertView吊舱 一切都很好,但是有了这个alertView,我也得到了这样一个提示:警告:尝试在已经呈现的内容上进行呈现 我怎样才能修好它 我不使用故事板或Xib文件。所有内容都是以编程方式编写的。 我尝试了使用谷歌找到的不同的解决方案——对我来说什么都不管用 附言。 我在下面附上了我的代码。我有两个视图控制器: 第一个viewController具有开始按钮: class F
我尝试了使用谷歌找到的不同的解决方案——对我来说什么都不管用 附言。 我在下面附上了我的代码。我有两个视图控制器: 第一个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)
}
}
}
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)
}
}
干杯 已解决
我之所以会出现这个错误,是因为只要“秒==0”,我就开始不断地调用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)
}
}
}
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。
内部视图显示我再次将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)
}
}
}
所以,它是有效的…但是现在我得到了另一个警告:尝试显示不在窗口层次结构中的视图代码>
有什么想法吗?解决了
我之所以会出现这个错误,是因为只要“秒==0”,我就开始不断地调用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)
}
}
}
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。
内部视图显示我再次将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)
}
}
}
所以,它是有效的…但是现在我得到了另一个警告:尝试显示不在窗口层次结构中的视图代码>
有什么想法吗?我不知道使用JSAlertview,但可能会有帮助谢谢你Mike Alter,但有点不同……我的朋友刚刚帮助我解决了这个问题。我不知道使用JSAlertview,但可能会有帮助谢谢你Mike Alter,但有点不同……我的朋友刚刚帮助我解决了这个问题。