3

我需要检查您的 wifi 状态是否在整个应用程序中发生了变化。我正在使用可达性来检查 wifi 状态是否打开。

我已经建立了这样的观察者:

[[NSNotificationCenter defaultCenter] addObserver: self selector: @selector(reachabilityChanged:) name: kReachabilityChangedNotification object: nil];

问题是我需要将此 addObserver 和 removeObserver 添加到所有视图控制器,并将reachabilityChanged 函数添加到所有。

是否有更好的方法然后添加 NSNotification 是否检查整个应用程序的 wifi 状态?

在这方面需要一些指导和建议。谢谢。

4

2 回答 2

10

调用并在super viewController中初始化并在中删除rootViewControllersubClassUIViewControllerinitNotificationdeallocNotification

然后所有你viewController应该subClassrootViewController。这只是OOP

像 :

@interface RootViewController : UIViewController

@end

@implementation RootViewController

- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}
- (id)init
{
    self = [super init];
    if (self) {

        [[NSNotificationCenter defaultCenter] addObserver: self selector: @selector(reachabilityChanged:) name: kReachabilityChangedNotification object: nil];

    }
    return self;
}
- (void)reachabilityChanged:(NSNotification *)notification
{
    // you can do nothing , it should be override.
}

And when you creat your viewController , you should subclass the RootViewController

@interface YourViewController : RootViewController

- (void)reachabilityChanged:(NSNotification *)notification
{
    // if your super class method do some things you should call [super reachabilityChanged:notification]
    // do your thing.
}

@end

In the implementation you should achieve the reachabilityChanged: method

于 2013-03-05T03:51:15.690 回答
1

I would make a UIViewController category and expose methods that subscribe and unsubscribe from the notification at hand, and also provide a default implementation of what needs to happen in the case of reachability being lost. The advantage of a category over inserting a new class into the inheritance hierarchy is that you can have subclasses of framework view controllers, e.g. UITableViewControllers, take advantage of the methods.

Example category and view controller code follows:

@interface UIViewController (CVReachability)

- (void)cvObserveReachability;
- (void)cvUnobserveReachability;

- (void)cvHandleReachabilityNotification:(NSNotification *)notification;

@end

@implementation UIViewController (CVReachability)

- (void)cvObserveReachability
{
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(cvHandleReachabilityNotification:)
                                                 name:REACHABILITY_NOTIFICATION_NAME
                                               object:nil];
}

- (void)cvUnobserveReachability
{
    [[NSNotificationCenter defaultCenter] removeObserver:self
                                                    name:REACHABILITY_NOTIFICATION_NAME
                                                  object:nil];
}

- (void)cvHandleReachabilityNotification:(NSNotification *)notification
{
    /*************************************************************
     * Anything that happens in this method should be appropriate for
     * every UIViewController in your application.
     ************************************************************/
    NSLog(@"Reachability notification handler: %@", notification);
}

@end

@implementation ViewController

...

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];

    [self cvObserveReachability];
}

- (void)viewDidDisappear:(BOOL)animated
{
    [super viewDidDisappear:animated];

    [self cvUnobserveReachability];
}

- (void)cvHandleReachabilityNotification:(NSNotification *)notification
{
    [super cvHandleReachabilityNotification:notification];

    /*****************************************************************
     * Do whatever is appropriate response for this view controller's 
     * area of responsibility. Perhaps show an alert...
     *****************************************************************/
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Connection Problem" message:@"Maybe your internet is down?" delegate:nil cancelButtonTitle:@"Dismiss" otherButtonTitles:nil];
    [alert show];
}

...

@end
于 2013-03-05T05:43:36.890 回答