我想使用我的应用程序的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 票数: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调用中方法名称后的冒号。

  ask by Joseph Toronto translate from so

未解决问题?本站智能推荐: