如何在全球范围内创建uialertcontroller [英] How to create uialertcontroller in global swift
问题描述
我正在尝试在 Config.swift
文件中创建 uialertcontroller
,如下所示。
I'm trying to create uialertcontroller
in Config.swift
file as follow.
static func showAlertMessage(titleStr:String, messageStr:String) -> Void {
let window : UIWindow?
let alert = UIAlertController(title: titleStr, message: messageStr, preferredStyle: UIAlertControllerStyle.Alert);
self.window!.presentViewController(alert, animated: true, completion: nil)
}
问题是我在 self.window中发现问题!。
类型'Config'没有会员'窗口'
Type 'Config' has no member 'window'
请告诉我如何解决该问题。
Please let me know how to solve that issue.
推荐答案
self.window
意味着有一个窗口
此类中的对象,情况并非如此。
self.window
would mean that there's a window
object in this class, and it's not the case.
您需要使用 let窗口:UIWindow?
带有窗口?.presentViewController(alert,animated:true,completion:nil)
,但这没有用,因为这个窗口实际上并不代表任何现有的窗口,无论如何它都不是视图控制器。
You would need to use your let window : UIWindow?
with window?.presentViewController(alert, animated: true, completion: nil)
, but this won't help, since this window does not actually represent any existing window, and it's not a view controller anyway.
所以我建议你将实际的视图控制器传递给方法:
So I suggest you pass the actual view controller you'll be using to the method:
static func showAlertMessage(vc: UIViewController, titleStr:String, messageStr:String) -> Void {
let alert = UIAlertController(title: titleStr, message: messageStr, preferredStyle: UIAlertControllerStyle.Alert);
vc.presentViewController(alert, animated: true, completion: nil)
}
你从一个UIViewController对象可用的类中调用它。
and you call it from a class where a UIViewController object is available.
这篇关于如何在全球范围内创建uialertcontroller的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!