我有以下代码,用于在当前可见 View (self.view
) 上显示操作表
[actionSheet showInView:[self view]];
但我无法使用以下方法在应用程序委托(delegate)中获取对此操作表的引用:
UIView *topView = [[self.window subviews] lastObject];
最佳答案
我相信操作表并没有真正添加为 subview :
- (void)didPresentActionSheet:(UIActionSheet *)actionSheet
{
NSLog(@"%@, %@", actionSheet.superview, self.view);
}
因此,其中一种方法是在显示操作表时发布通知;例如:
// delegate = self;
- (void)didPresentActionSheet:(UIActionSheet *)actionSheet
{
[[NSNotificationCenter defaultCenter] postNotificationName:@"ActionSheetPresented" object:nil userInfo:@{@"actionSheet": actionSheet}];
}
并在AppDelegate
中添加通知的观察者
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(displayed:) name:@"ActionSheetPresented" object:nil];
- (void)displayed:(NSNotification *)notification
{
UIActionSheet *action = notification.userInfo[@"sheet"];
NSLog(@"%@", action);
}
您也可以将其保留为 View Controller 中的公共(public)属性,并在 didPresentActionSheet:
委托(delegate) API 触发时在 AppDelegate
中引用。
关于ios - 查找顶部显示的 View UIActionSheet,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29439280/