Reputation: 36013
I see a lot of questions about this on SO but their answers do nothing to me.
I am presenting a popover using storyboard. After a while I want to dismiss that popover programmatically.
I have tried many things but the last try involves creating a class for the view controller that is inside the popover. The class is like this:
- (id)initWithCoder:(NSCoder *)aDecoder {
self = [super initWithCoder:aDecoder];
if (self) {
[self initializeNotification];
}
return self;
}
- (void) initializeNotification {
[[NSNotificationCenter defaultCenter]
addObserverForName:@"closePopover"
object:self
queue:[NSOperationQueue mainQueue]
usingBlock:^(NSNotification * _Nonnull note) {
[self dismissViewControllerAnimated:YES completion:nil];
}];
}
then, from the main code I post a notification like
[[NSNotificationCenter defaultCenter]
postNotificationName:@"closePopover"
object:self];
and nothing happens... the popover continues there.
why?
Upvotes: 0
Views: 45
Reputation: 14040
You have to replace self
with nil
(for the object
parameter) when creating the notification observer since it is not self
that posts the notification:
[[NSNotificationCenter defaultCenter] addObserverForName:@"closePopover" object:nil queue:[NSOperationQueue mainQueue] usingBlock:^(NSNotification * _Nonnull note) {
[self dismissViewControllerAnimated:YES completion:nil];
}];
Upvotes: 2