在视图之间发送NSNotification

Jon*_*rft 0 objective-c nsnotifications nsnotificationcenter ios

这是我的第一次尝试NSNotification,试过几个教程,但不知怎的,它不起作用.

基本上我正在向B类发送一个字典,它是弹出子视图(UIViewController)并测试是否已收到.

谁能告诉我我做错了什么?

A级

- (IBAction)selectRoutine:(id)sender {
    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Storyboard" bundle:nil];

    NSDictionary *dictionary = [NSDictionary dictionaryWithObject:@"Right"
                                                           forKey:@"Orientation"];
    [[NSNotificationCenter defaultCenter]
     postNotificationName:@"PassData"
     object:nil
     userInfo:dictionary];

    createExercisePopupViewController* popupController = [storyboard instantiateViewControllerWithIdentifier:@"createExercisePopupView"];

    //Tell the operating system the CreateRoutine view controller
    //is becoming a child:
    [self addChildViewController:popupController];

    //add the target frame to self's view:
    [self.view addSubview:popupController.view];

    //Tell the operating system the view controller has moved:
    [popupController didMoveToParentViewController:self];

}
Run Code Online (Sandbox Code Playgroud)

B级

- (void)viewDidLoad
{
    [super viewDidLoad];

    [[NSNotificationCenter defaultCenter]
     addObserver:self
     selector:@selector(receiveData:)
     name:@"PassData"
     object:nil];
}

- (void)receiveData:(NSNotification *)notification {
    NSLog(@"Data received: %@", [[notification userInfo] valueForKey:@"Orientation"]);
}
Run Code Online (Sandbox Code Playgroud)

ban*_*isa 5

如果它还没有注册接收该通知 - 它将永远不会收到它.通知不会持续存在.如果没有已注册的侦听器,则发布的通知将丢失.