来自NSObject子类的IOS NSNotificationCenter addObserver

时间:2015-10-01 10:14:05

标签: ios objective-c nsnotificationcenter nsnotifications

我添加了AppDelegate的观察者,并将此代码放在didFinishLaunchingWithOptiong方法中。

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reachabilityChanged:) name:kReachabilityChangedNotification object:nil];
internetReachability = [Reachability reachabilityForInternetConnection];
[internetReachability startNotifier];

现在我想将此addObserver放在我的自定义类中,该类是NSObject的子类?

编辑:

这是我的reachabilityChanged方法:

- (void) reachabilityChanged:(NSNotification *)note
{
    NSLog(@"change");
}

我将此方法放在我调用addObserver

的同一个类中

1 个答案:

答案 0 :(得分:0)

要运行reachabilityChanged方法,您需要在AppDelegate中发布已注册的通知,如下所示

[[NSNotificationCenter defaultCenter] postNotificationName:kReachabilityChangedNotification object:nil];