1

我现在正在学习通知编程,有一个非常简单的项目有两个类有一点问题,在发布通知时不调用通知选择器方法。这很奇怪,希望有人帮我找出问题所在,我非常感谢!

我的源代码:

视图控制器.h

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController

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

- (IBAction)presentViewController:(id)sender;

@end

视图控制器.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

视图控制器2.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
4

2 回答 2

2

我在您的代码中发现了两个问题

1)

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

您在此处发布通知,然后显示ViewController2,您在此处发送了通知,但在当前时间点没有可用的接收器。所以什么都不会发生。

检查:

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

为什么又要分配ViewController

只需从presentViewController:方法中传递它。

于 2013-05-29T18:34:33.470 回答
2

你有2个问题:

  1. 您在任何人注册接收通知之前发布通知。
  2. 添加观察者时,您过滤到不发布通知 ( object:self.viewController) 的实例。

对于 1. 不存储通知,它们仅由发布通知时附加的观察者接收。

对于 2. 可以使用对象发布通知,并且观察者可以过滤该对象。如果对象不匹配,则不会调用该方法。如果您不想进行任何过滤,请在观察时将对象设置为 nil。

交换视图控制器演示和通知发布的顺序:

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

    [self presentViewController:viewController2
                   animated:YES
                   completion:^{
[[NSNotificationCenter defaultCenter] postNotificationName:@"networkNotification"
                                                    object:nil];
}
于 2013-05-29T18:32:27.607 回答