如何正确设置NSNotification并发布?

时间:2013-12-13 08:08:19

标签: ios objective-c

我想为每个连接委托制作一个单例可达性观察器, 但是我无法正确构建它,这里有一些代码片段。

MYReachability.m

static MYReachability *sharedInstance;

+ (MYReachability *)sharedInstance
{
    if (sharedInstance == NULL) {
        sharedInstance = [[MYReachability alloc] init];
    }

    return sharedInstance;
}

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

    Reachability *reach = [Reachability reachabilityWithHostname: @"www.apple.com"];
    [reach startNotifier];
}

- (void) reachabilityChanged: (NSNotification *)notification {
    NSLog(@"notification: %@", notification);
    Reachability *reach = [notification object];

    if( [reach isKindOfClass: [Reachability class]]) {
        NetworkStatus status = [reach currentReachabilityStatus];
        NSLog(@"reachability status: %u", status);
        if (status == NotReachable) {
            // Insert your code here
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error"
                                                            message:@"Cannot connect to server..."
                                                           delegate:nil
                                                  cancelButtonTitle:@"OK"
                                                  otherButtonTitles:nil];
            [alert show];
        }
    }
}

在MYConnectionDelegate.m

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
    if (error.code) {
        // sending notification to viewController
        [[MYReachability sharedInstance] addReachabilityObserver];
        MYTestClass *myTestClass = [MYTestClass new];
        [myTestClass notificationTrigger];
    }
}

在MYTestClass.m

- (void)notificationTrigger
{
    // All instances of TestClass will be notified
    [[NSNotificationCenter defaultCenter]
     postNotificationName:kReachabilityChangedNotification
     object:self];
}

瑞克,谢谢,这是有效的,但这是另一个问题, 每次调用都会再生成一个堆栈通知...... 我在MYReachability.m

中做了一个dealloc
- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

但之前的通知仍然存在。

2 个答案:

答案 0 :(得分:2)

您正在调用方法notificationTrigger,但您的测试类只有方法notificationTrigger:带冒号,即一个参数。

[myTestClass notificationTrigger];更改为[myTestClass notificationTrigger:self];,它应该有效。

如果您只希望通知显示一次,请在显示警报视图后以观察者身份移除自己,如下所示:

- (void) reachabilityChanged: (NSNotification *)notification {
NSLog(@"notification: %@", notification);
Reachability *reach = [notification object];

if( [reach isKindOfClass: [Reachability class]]) {
    NetworkStatus status = [reach currentReachabilityStatus];
    NSLog(@"reachability status: %u", status);
    if (status == NotReachable) {
        // Insert your code here
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error" message:@"Cannot connect to server..." delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
        [alert show];
        [[NSNotificationCenter defaultCenter] removeObserver:self];
        }
    }
}

答案 1 :(得分:2)

在一堂课中:

[[NSNotificationCenter defaultCenter] postNotificationName:@"notifyMe" object:self];

在另一堂课:

[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(doSomething)
                                             name:@"notifyMe"
                                           object:nil];