简体   繁体   English

iOS NSNotification简单的Xcode项目问题

[英]iOS NSNotification Simple Xcode Project Problems

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 ViewController.h

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController

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

- (IBAction)presentViewController:(id)sender;

@end

ViewController.m 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 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 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: 你有2个问题:

  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 ). 添加观察者时,您将过滤到不发布通知的实例( object:self.viewController )。

For 1. Notifications aren't stored, they are received only by observers attached when the notification is posted. 对于1.通知未存储,只有在发布通知时附加的观察者才会收到通知。

For 2. Notifications can be posted with an object and observers can filter on that object. 对于2.通知可以与对象一起发布,观察者可以对该对象进行过滤。 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. 在观察您是否不想进行任何过滤时,将对象设置为nil。

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) 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. 在这里,您发布通知然后显示ViewController2 ,此处您发送了通知,但当前时间没有可用的接收器。 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) 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 ? 你为什么再次分配ViewController

Just pass it from the presentViewController: method. 只需从presentViewController:方法传递它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM