当我在当前警报行中使用警告时,我会得到这个错误,使用未解决的标识符。
文件名:CommonFunctions.swift
类名:类CommonFunctions : NSObject
我正在使用这个代码
class func gotoSettingScreen()
{
let alertController = UIAlertController (title: "Title", message: "Go to Settings?", preferredStyle: .alert)
let settingsAction = UIAlertAction(title: "Settings", style: .default) { (_) -> Void in
guard let settingsUrl = URL(string: UIApplicationOpenSettingsURLString) else {
return
}
if UIApplication.shared.canOpenURL(settingsUrl) {
if #available(iOS 10.0, *) {
UIApplication.shared.open(settingsUrl, completionHandler: { (success) in
print("Settings opened: \(success)") // Prints true
})
} else {
// Fallback on earlier versions
}
}
}
alertController.addAction(settingsAction)
let cancelAction = UIAlertAction(title: "Cancel", style: .default, handler: nil)
alertController.addAction(cancelAction)
present(alertController, animated: true, completion: nil)
}行错误来:present(alertController, animated: true, completion: nil)
错误是:

谢谢你的帮助!还有欣赏!
发布于 2017-11-24 06:43:42
您需要一个ViewController的引用,您需要在其中显示警报。由于您使用的是自定义类NSObject: CommonFunctions,当您要显示警报时,您需要传入视图控制器的实例,或者将视图控制器设置为CommonFunctions类的属性之一。
示例:
// Basic.Swift
......
var viewController : UIViewController?调用该方法以显示警报时,请将视图控制器属性设置为当前实例,如下所示
CommonFunctions.viewController = self您必须修改CommonFunctions的getSettingScreen()函数才能使用
viewController.present()而不是present()
这会有帮助的。
发布于 2018-10-08 11:14:34
如果您想使用来自非uiview控制器的警报,可以传递uiviewcontroller实例表单方法,如
方法在NonUicontroller类中的应用
func alertDilog(viewController :UIViewController)
{
let alertController = UIAlertController(title: title, message: msg, preferredStyle: UIAlertControllerStyle.alert)
let okAction = UIAlertAction(title: "OK", style: UIAlertActionStyle.default) {
(result : UIAlertAction) -> Void in
print("OK")
alertController.addAction(okAction)
vc.present(alertController, animated: true, completion: nil)
}
}在UiViewcontrollerclass中使用alertDialog
yourclassName.alertDilog(viewController :self)https://stackoverflow.com/questions/47467537
复制相似问题