AppDelegate没有响应NSNotification

时间:2014-04-19 22:06:01

标签: ios objective-c cocoa-touch nsnotificationcenter appdelegate

我想使用我的应用程序的AppDelegate发布CRToast通知

以下是我添加观察者的方法。

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
[[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(noConnectionNotification)
                                                 name:@"No_Connection_Available" object:nil];

}

这是它应该调用的方法

-(void)noConnectionNotification{
     NSLog(@"received notification");
    NSString *notificationString = @"Unable to reach server. Changes will be cached.";
    NSDictionary *options = @{kCRToastTextKey : notificationString,
                              kCRToastNotificationTypeKey : @(CRToastTypeStatusBar),
                              kCRToastTextAlignmentKey : @(NSTextAlignmentCenter),
                              kCRToastBackgroundColorKey : [UIColor redColor],
                              kCRToastAnimationInDirectionKey : @(CRToastAnimationDirectionTop),
                              kCRToastAnimationOutDirectionKey : @(CRToastAnimationDirectionTop),
                              kCRToastTimeIntervalKey : @(2),
                              };
    [CRToastManager showNotificationWithOptions:options
                                completionBlock:nil];
}

以及我如何从其他班级发布通知:

 dispatch_async(dispatch_get_main_queue(), ^{
       [[NSNotificationCenter defaultCenter] postNotificationName:@"No_Connection_Available" object:nil];
        });
    }

不会调用该方法。如果我将我的各种视图控制器中的一个作为观察者,那个获取通知,但AppDelegate不是。是不是可以让AppDelegate成为NSNotification观察者?

1 个答案:

答案 0 :(得分:4)

接收通知时调用的方法需要有NSNotification参数。因此,该方法应如下所示:

-(void)noConnectionNotification:(NSNotification*)notification {
     NSLog(@"received notification");
    NSString *notificationString = @"Unable to reach server. Changes will be cached.";
    NSDictionary *options = @{kCRToastTextKey : notificationString,
                              kCRToastNotificationTypeKey : @(CRToastTypeStatusBar),
                              kCRToastTextAlignmentKey : @(NSTextAlignmentCenter),
                              kCRToastBackgroundColorKey : [UIColor redColor],
                              kCRToastAnimationInDirectionKey : @(CRToastAnimationDirectionTop),
                              kCRToastAnimationOutDirectionKey : @(CRToastAnimationDirectionTop),
                              kCRToastTimeIntervalKey : @(2),
                              };
    [CRToastManager showNotificationWithOptions:options
                                completionBlock:nil];
}

然后,添加观察者应该如下所示:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
[[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(noConnectionNotification:)
                                                 name:@"No_Connection_Available" object:nil];

}

请注意@selector调用中方法名称后面的冒号。