gpt4 book ai didi

swift - AlertController 尝试在 View 上而不是在窗口层次结构中呈现

转载 作者:行者123 更新时间:2023-11-30 11:49:28 24 4
gpt4 key购买 nike

试图找出为什么我会收到此错误。我有一个带有两个 View Controller 的单 View 应用程序。我通过触摸按钮转到第二个 vc:

@IBAction func showAccount(_ sender: Any) {
performSegue(withIdentifier: "toAccountFromRoot", sender: self)
}

我有一个使用 Controller 文件和几个帮助程序文件设置的外观模式。我在 AccountViewController 文件上设置 Controller :

let myController = Controller.sharedInstance

帮助程序文件之一是设置一个中央 AlertController 以简化警报(没有额外的按钮)。它有一种方法,presentAlert,它接受一些参数、警报标题、消息、演示者。

在我的 AccountViewController 上,我尝试使用以下代码显示警报:

myController.alert.presentAlert("No Agent", "You have not registered an agent yet.", self)

在 AlertManager 文件中,presentAlert 如下所示:

func presentAlert(_ title:String, _ message:String, _ presenter:UIViewController) {

let myAlert = UIAlertController(title: title, message: message, preferredStyle: .alert)

let okAction = UIAlertAction(title: "OK", style: .default, handler: nil)
myAlert.addAction(okAction)

presenter.present(myAlert, animated:true, completion:nil)
}

我已经在很多应用程序中完成了此操作,没有出现任何问题,因此我很困惑为什么编译器认为 AccountViewController View 不在窗口层次结构中。

最佳答案

该死,输入了所有内容,然后我在 5 秒后解决了它。以防万一其他人遇到同样的问题,我试图在 viewDidLoad 中呈现警报,应该在 viewDidAppear 中这样做。

关于swift - AlertController 尝试在 View 上而不是在窗口层次结构中呈现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48487326/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com