试图解除故事板

时间:2017-08-22 16:19:38

标签: ios uistoryboard uipopovercontroller dismiss

我在SO上看到很多关于这个的问题,但他们的答案对我没有任何意义。

我正在使用故事板呈现一个popover。过了一会儿,我想以编程方式解雇那个popover。

我尝试了很多东西,但最后一次尝试涉及为popover中的视图控制器创建一个类。课程是这样的:

- (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];
   }];
}

然后,从主要代码我发布了一个通知,如

  [[NSNotificationCenter defaultCenter]
    postNotificationName:@"closePopover"
                  object:self];

没有任何反应......弹出窗口继续存在。

为什么?

1 个答案:

答案 0 :(得分:2)

在创建通知观察者时,您必须将self替换为nil(对于object参数),因为它不是self发布通知:

[[NSNotificationCenter defaultCenter] addObserverForName:@"closePopover" object:nil queue:[NSOperationQueue mainQueue] usingBlock:^(NSNotification * _Nonnull note) {
    [self dismissViewControllerAnimated:YES completion:nil];
}];
相关问题