IOS Swift处理全球事件 [英] IOS Swift handle global events

查看:121
本文介绍了IOS Swift处理全球事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何处理通知中心触发的全局事件,例如在我的API类中,如果接收到错误响应,则触发事件。 (500)。当该事件触发时,UIAlert应显示在任何视图控制器处于活动状态,或者在退出登录视图控制器时应显示。

How can I handle global events triggered by the notification centre for example in my API class I fire an event if an error response is received e.g. (500). When that event is fired an UIAlert should be displayed on what ever view controller is active, or on logout the login view controller should be presented.

据我看到,为了与当前的视图控制器进行交互,没有简单的方法来获取当前的视图控制器。 (请注意,我的根视图控制器是导航控制器

As far as I can see there is no easy way to get the current view controller in order to interact with it. (Note that my root view controller is NOT a navigation controller).

推荐答案

难以获得当前视图控制器(不使用导航控制器时)?

Is this way too hard to get current view controller ( when not using navigation controller ) ?

// on your app delegate    
getCurrentViewController(self.window!.rootViewController!)

func getCurrentViewController(viewController:UIViewController)-> UIViewController{

    if let navigationController = viewController as? UINavigationController{
       return getCurrentViewController(navigationController.visibleViewController)
    }

    if let viewController = viewController?.presentedViewController {

        return getCurrentViewController(viewController)

    }else{

        return viewController
    }
}

这篇关于IOS Swift处理全球事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆