How to show an alert from another class in Swift?

前端 未结 3 509
我寻月下人不归
我寻月下人不归 2021-02-02 02:11

I have a main class, AddFriendsController, that runs the following line of code:

ErrorReporting.showMessage(\"Error\", msg: \"Could not add student          


        
3条回答
  •  遥遥无期
    2021-02-02 02:48

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

提交回复
热议问题