簡體   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