I have a main class, AddFriendsController
, that runs the following line of code:
ErrorReporting.showMessage("Error", msg: "Could not add student to storage.")
I then have this ErrorReporting.swift
file:
import Foundation
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)
}
}
Obviously, self
wouldn't work here, and is giving me an error. How can I refer to the currently open view controller (i.e. AddFriendsController
in this circumstance), as I am wishing to use this same method in many different swift files?
Thanks.
Actually, in my opinion the view controller presenting operation should be done on the
UIViewController
instance, not in a model class.A simple workaround for it is to pass the
UIViewController
instance as a parameterAnd call it like below
You can create extension method for UIApplication (for example) which will return your topViewController:
And then your class will look like this:
Method need to be
static
to be able to call it asErrorReporting.showMessage
.Swift 3 version of Maksym Musiienko's answer would be the following: