Ios 如何正确设置NSNotification和post?

Ios 如何正确设置NSNotification和post?,ios,objective-c,Ios,Objective C,我想为每个要使用的连接委托创建一个单例可达性观察器, 然而,我无法正确构建它,下面是一些代码片段 我的可达性 static MYReachability *sharedInstance; + (MYReachability *)sharedInstance { if (sharedInstance == NULL) { sharedInstance = [[MYReachability alloc] init]; } return sharedInst

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

我的可达性

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];
        }
    }
}
- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}
在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];
}
瑞克,谢谢,这很有效,但还有一个问题, 每次调用都会在堆栈中生成一个或多个通知。。。 我在我的可达性上做了一个交易

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

您正在调用方法
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];
        }
    }
}
在一类中:

[[NSNotificationCenter defaultCenter] postNotificationName:@"notifyMe" object:self];
在另一类中:

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

[[NSNotificationCenter defaultCenter] postNotificationName:@"notifyMe" object:self];
在另一类中:

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

或[myTestClass notificationTrigger:self];因为它应该是发送者,以防他以后想使用它。或者[myTestClass notificationTrigger:self];因为它应该是发送者,以防他以后想使用它。你在说什么堆栈?发送通知时,要执行的代码只运行一次。你是说有不止一个观察员?顺便问一下,通过将代码放在dealloc中,您想要实现什么?顺便说一句,这不是创建单例的方式。是的,alertView会显示多次,就像它触发的次数一样多,好像以前的通知没有发布一样。请参阅我的更新答案。
==NULL
是否应该更好地写为
==nil
?您在谈论什么堆栈?发送通知时,要执行的代码只运行一次。你是说有不止一个观察员?顺便问一下,通过将代码放在dealloc中,您想要实现什么?顺便说一句,这不是创建单例的方式。是的,alertView将显示不止一次,正如它触发的次数一样多,似乎以前的通知没有发布。请参阅我的更新答案。是否应该将
==NULL
写成
==nil
更好?