我想为每个要使用的连接委托创建一个单例可到达性观察器,
但是我无法正确构建它,这是一些代码片段。

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];
}

但是以前的通知仍然存在。

最佳答案

您正在调用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];
        }
    }
}

关于ios - 如何正确设置NSNotification并发布?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/20561822/

10-12 01:55