Это моя первая попытка с NSNotification
, попробовал несколько учебников, но почему-то это не работает.отправка NSNotification между видами
В основном я отправляю словарь в класс B, который является popview subview (UIViewController
) и проверяется, был ли он получен.
Может ли кто-нибудь сказать мне, что я делаю неправильно?
Класс
- (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"]);
}
Имеет класс B, загруженный до того, как класс A отправит уведомление? – bandejapaisa
Нет, он не загружен. Именно так мне нужно было переместить уведомление после его загрузки. Ответьте правильно, чтобы я мог отметить его как ответ. спасибо –
Переместить '[[NSNotificationCenter defaultCenter] postNotificationName: @" PassData " объект: nil userInfo: dictionary];' после того, как вы создали класс B – andreagiavatto