I sometimes create a utilities class to display alerts and such. What I usually do is have my methods for presenting view controllers take the current view controller as a parameter. That approach works pretty well.
EDIT:
Here is an example method from a file Utils.swift in one of my projects. It defines a class function that displays a UIAlertController alert on the current view controller:
class Utils
{
static let sharedUtils = Utils()
class func showAlertOnVC(
targetVC: UIViewController,
var title: String,
var message: String)
{
title = NSLocalizedString(title, comment: "")
message = NSLocalizedString(message, comment: "")
let alert = UIAlertController(
title: title,
message: message,
preferredStyle: UIAlertControllerStyle.Alert)
let okButton = UIAlertAction(
title:"OK",
style: UIAlertActionStyle.Default,
handler:
{
(alert: UIAlertAction!) in
})
alert.addAction(okButton)
targetVC.presentViewController(alert, animated: true, completion: nil)
}
}
The code above defines a class Utils. Note that it does not have any base class, which is Ok in Swift.
Next it defines a public static variable sharedUtils that you can use to get access to the singleton Utils class.
Finally, it defines a class method showAlertOnVC
that can be used to display a UIAlertController alert on top of the current view controller. To use showAlertOnVC
you call it from the current view controller and pass self as the targetVC
parameter.