Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to show an alert from another class in Swift?

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.

like image 209
Lucas Avatar asked Oct 13 '16 07:10

Lucas


2 Answers

You can create extension method for UIApplication (for example) which will return your 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
    }
}

And then your class will look like this:

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)
    }
}

Method need to be static to be able to call it as ErrorReporting.showMessage.

like image 74
Maksym Musiienko Avatar answered Oct 29 '22 23:10

Maksym Musiienko


Swift 3 version of Maksym Musiienko's answer would be the following:

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
    }
}
like image 26
fgroeger Avatar answered Oct 29 '22 22:10

fgroeger