繁体   English   中英

如何正确设置NSNotification并发布?

[英]How to correctly setup NSNotification and post?

我想为每个连接委托制作一个单独的Reachability观察者,但是我无法正确构建它,这里有一些代码片段。

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

Rick,谢谢,这是有效的,但是这里有另一个问题,每次调用都会再生成一个堆栈通知......我在MYReachability.m中做了一个dealloc

- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

但以前的通知仍然存在。

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

改变[myTestClass notificationTrigger]; to [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];
        }
    }
}

在一个班级:

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

在另一个班级:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM