Luc*_*emy 9 uiviewcontroller swift uialertcontroller swift2.3
我有一个主类,AddFriendsController它运行以下代码行:
ErrorReporting.showMessage("Error", msg: "Could not add student to storage.")
Run Code Online (Sandbox Code Playgroud)
然后我有这个ErrorReporting.swift文件:
进口基金会
class ErrorReporting {
func showMessage(title: String, msg: String) {
let alert = UIAlertController(title: title, message: msg, preferredStyle: UIAlertControllerStyle.Alert)
alert.addAction(UIAlertAction(title: "Ok", style: UIAlertActionStyle.Default, handler: nil))
self.presentViewController(alert, animated: true, completion: nil)
}
}
Run Code Online (Sandbox Code Playgroud)
显然,self不会在这里工作,并给我一个错误.我如何引用当前打开的视图控制器(即AddFriendsController在这种情况下),因为我希望在许多不同的swift文件中使用相同的方法?
谢谢.
Mak*_*nko 27
您可以为UIApplication创建扩展方法(例如),它将返回您的topViewController:
extension UIApplication {
static func topViewController(base: UIViewController? = UIApplication.sharedApplication().delegate?.window??.rootViewController) -> UIViewController? {
if let nav = base as? UINavigationController {
return topViewController(nav.visibleViewController)
}
if let tab = base as? UITabBarController, selected = tab.selectedViewController {
return topViewController(selected)
}
if let presented = base?.presentedViewController {
return topViewController(presented)
}
return base
}
}
Run Code Online (Sandbox Code Playgroud)
然后你的课将如下所示:
class ErrorReporting {
static func showMessage(title: String, msg: String) {
let alert = UIAlertController(title: title, message: msg, preferredStyle: UIAlertControllerStyle.Alert)
alert.addAction(UIAlertAction(title: "Ok", style: UIAlertActionStyle.Default, handler: nil))
UIApplication.topViewController()?.presentViewController(alert, animated: true, completion: nil)
}
}
Run Code Online (Sandbox Code Playgroud)
方法需要static能够将其称为ErrorReporting.showMessage.
实际上,在我看来,视图控制器呈现操作应该在UIViewController实例上完成,而不是在模型类中完成.
一个简单的解决方法是将UIViewController实例作为参数传递
class ErrorReporting {
func showMessage(title: String, msg: String, `on` controller: UIViewController) {
let alert = UIAlertController(title: title, message: msg, preferredStyle: UIAlertControllerStyle.Alert)
alert.addAction(UIAlertAction(title: "Ok", style: UIAlertActionStyle.Default, handler: nil))
controller.presentViewController(alert, animated: true, completion: nil)
}
}
Run Code Online (Sandbox Code Playgroud)
并在下面调用它
ErrorReporting.showMessage("Error", msg: "Could not add student to storage.", on: self)
Run Code Online (Sandbox Code Playgroud)
小智 7
Swift 3版本的Maksym Musiienko的答案如下:
extension UIApplication {
static func topViewController(base: UIViewController? = UIApplication.shared.delegate?.window??.rootViewController) -> UIViewController? {
if let nav = base as? UINavigationController {
return topViewController(base: nav.visibleViewController)
}
if let tab = base as? UITabBarController, let selected = tab.selectedViewController {
return topViewController(base: selected)
}
if let presented = base?.presentedViewController {
return topViewController(base: presented)
}
return base
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5398 次 |
| 最近记录: |