3

アプリ全体で Wi-Fi の状態が変化したかどうかを確認する必要があります。到達可能性を使用して、wifi ステータスがオンになっているかどうかを確認しています。

次のようなオブザーバーを設定しました。

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

問題は、この addObserver と removeObserver をすべてのビューコントローラーに追加し、reachabilityChanged 関数をすべてに追加する必要があることです。

アプリ全体でwifiステータスを確認するかどうかにかかわらず、NSNotificationを追加するよりも良い方法はありますか?

これに関するいくつかのガイダンスと提案が必要です。ありがとう。

4

2 に答える 2

10

super viewController呼び出し、を呼び出しrootViewControllersubClass、を初期化し、を削除します。UIViewControllerinitNotificationdeallocNotification

そして、あなたのすべてのviewController必要subClassがありrootViewControllerます。それだけ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.
}

そして、 viewController を作成するときは、サブクラス化する必要があります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

メソッドimplementationを達成する必要がありますreachabilityChanged:

于 2013-03-05T03:51:15.690 に答える
1

カテゴリを作成し、UIViewController目前の通知をサブスクライブおよびサブスクライブ解除するメソッドを公開し、到達可能性が失われた場合に発生する必要があることのデフォルトの実装も提供します。継承階層に新しいクラスを挿入することに対するカテゴリの利点は、UITableViewControllers などのフレームワーク ビュー コントローラのサブクラスでメソッドを利用できることです。

カテゴリとビュー コントローラーのコード例は次のとおりです。

@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 に答える