How to dismiss UIActionSheets and UIPopoverControllers without knowing who presented them

蹲街弑〆低调 提交于 2019-12-05 22:39:08
Xiaochen Du

I would register the UIPopover instance to listen to some notification.

[[NSNotificationCenter defaultCenter] addObserver:_myPopOver 
                                         selector:@selector(myDismissPopover)
                                             name:@"dismissPopover" 
                                           object:nil];

And add extension to UIPopover class.

- (void) myDismissPopover {
 [self dismissPopoverAnimated:YES];
}

When I need to dismiss popover, I just need to post notification.

[[NSNotificationCenter defaultCenter] postNotificationName:@"dismissPopover" 
                                                    object:nil];
aslisabanci

I'll write down my own solution as we've talked with bshirley in the comments of the question. I've implemented a mechanism like this to solve the problem:

In my login view controller, I create an NSMutableArray that will keep all my action sheets and popover controllers that are going to be dismissed. Then I store this array in a global dictionary. I access this dictionary via a utility method. Then all through the application, whoever creates an action sheet or a popover controller, adds the component to this array (retrieves the array from the global data, modifies it and then saves it back to the global data). Then when the user is thrown back to the login screen, in viewWillDisappear of my login view controller, I loop through this array and call the appropriate dismiss method by checking if the UIView I get from the array is an action sheet or a popover controller. Then I remove all the elements of this array and then store it back in the global data, again.

Hope this helps anyone who needs to implement a similar mechanism. Your comments will be appreciated.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!