2017-08-22 66 views
0

我看到很多关于这个的问题,但他们的答案对我没有任何帮助。试图解雇storyboard提供的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]; 

,并没有任何反应......一个通知酥料饼的继续存在。

为什么?

+0

分享完整的代码,如果你能 – Krunal

+0

这是全码。我点击一个按钮,按钮显示一个从这个类ViewController。我试图在稍后解雇。它什么也没做。 – SpaceDog

回答

2

你必须创建通知观察者时,因为它不符合nil更换self(为object参数)self该帖子通知:

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

BRILLIANT?我认为这个对象是通知被分配到的地方!谢谢!现在完美的工作! – SpaceDog

+1

很高兴我能帮忙:) –