这是我第一次尝试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];
}
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"]);
}