سؤال

I'm now learning notification programming, have a very simple project have two classes that have a little problems that dont't call notification selector method when posting notification. That's very weird, hope someone help me find where problems occurred, i'm very appreciated that!

My source code:

ViewController.h

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController

@property (weak, nonatomic) IBOutlet UIButton *pushButton;

- (IBAction)presentViewController:(id)sender;

@end

ViewController.m

#import "ViewController.h"
#import "ViewController2.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}

- (IBAction)presentViewController:(id)sender
{
    [[NSNotificationCenter defaultCenter] postNotificationName:@"networkNotification"
                                                        object:self];
    ViewController2 *viewController2 = [self.storyboard instantiateViewControllerWithIdentifier:@"viewController2"];
    [self presentViewController:viewController2
                       animated:YES
                     completion:nil];
}

@end

ViewController2.h

#import <UIKit/UIKit.h>

@class ViewController;

@interface ViewController2 : UIViewController

@property (weak, nonatomic) IBOutlet UILabel *testLabel;
@property (nonatomic, strong) ViewController *viewController;

@end

ViewController2.m

#import "ViewController2.h"
#import "ViewController.h"

@interface ViewController2 ()

@end

@implementation ViewController2

@synthesize testLabel;

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.viewController = [[ViewController alloc] init];
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(recievingNotifications:)
                                                 name:@"networkNotification"
                                               object:self.viewController];
}

- (void)recievingNotifications:(NSNotification *)aNotification
{
    if ([[aNotification name] isEqualToString:@"networkNotification"])
    {
        [UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
        self.testLabel.text = @"Good";
    }
}

- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self
                                                    name:@"networkNotification"
                                                  object:self.viewController];
}

@end
هل كانت مفيدة؟

المحلول

You have 2 problems:

  1. You post the notification before anyone is registered to receive it.
  2. When adding the observer you filter to an instance that doesn't post a notification (object:self.viewController).

For 1. Notifications aren't stored, they are received only by observers attached when the notification is posted.

For 2. Notifications can be posted with an object and observers can filter on that object. If the objects don't match the method won't be called. Set the object to nil when observing if you don't want any filtering done.

Swap the order of your view controller presentation and the notification posting:

ViewController2 *viewController2 = [self.storyboard instantiateViewControllerWithIdentifier:@"viewController2"];

    [self presentViewController:viewController2
                   animated:YES
                   completion:^{
[[NSNotificationCenter defaultCenter] postNotificationName:@"networkNotification"
                                                    object:nil];
}

نصائح أخرى

There are two issues I found on your code

1)

- (IBAction)presentViewController:(id)sender
{
    [[NSNotificationCenter defaultCenter] postNotificationName:@"networkNotification"
                                                        object:self];
    ViewController2 *viewController2 = [self.storyboard instantiateViewControllerWithIdentifier:@"viewController2"];
    [self presentViewController:viewController2
                       animated:YES
                     completion:nil];
}

Here you are posting a notification then displays the ViewController2, here you sent the notification but there is no receiver available at the current point of time. So nothing will happen.

Check with:

- (IBAction)presentViewController:(id)sender
{
    ViewController2 *viewController2 = [self.storyboard instantiateViewControllerWithIdentifier:@"viewController2"];
    [self presentViewController:viewController2
                       animated:YES
                     completion:nil];
    [[NSNotificationCenter defaultCenter] postNotificationName:@"networkNotification"
                                                        object:self];
}

2)

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.viewController = [[ViewController alloc] init];
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(recievingNotifications:)
                                                 name:@"networkNotification"
                                               object:self.viewController];
}

Why are you again allocating ViewController ?

Just pass it from the presentViewController: method.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top