【问题标题】:Trying to dismiss a popover that was presented by storyboard试图关闭故事板呈现的弹出框
【发布时间】:2017-08-22 16:19:38
【问题描述】:

我在 SO 上看到很多关于此的问题,但他们的回答对我没有任何帮助。

我正在使用情节提要展示一个弹出框。过了一会儿,我想以编程方式关闭该弹出框。

我已经尝试了很多东西,但最后一次尝试涉及为弹出框内的视图控制器创建一个类。类是这样的:

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

什么都没有发生...弹出窗口继续在那里。

为什么?

【问题讨论】:

  • 分享完整代码,如果可以的话
  • 这是完整的代码。我单击一个按钮,该按钮显示来自此类的 ViewController。我稍后会尝试忽略它。它什么都不做。

标签: ios uistoryboard uipopovercontroller dismiss


【解决方案1】:

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

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

【讨论】:

  • 出色?我认为这个对象是分配通知的对象!谢谢!现在完美运行!!!
  • 很高兴我能帮上忙 :)
猜你喜欢
  • 2012-04-30
  • 1970-01-01
  • 2012-05-02
  • 2013-02-08
  • 2014-11-09
  • 1970-01-01
  • 2021-06-05
  • 2014-12-04
  • 2012-10-21
相关资源
最近更新 更多