ios - 查找顶部显示的 View UIActionSheet

标签 ios uiview uiactionsheet appdelegate uiwindow

我有以下代码,用于在当前可见 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/

相关文章:

swift - 在 SWIFT 中使用 UIDatePicker(在 UIActionSheet 中)输入日期时 UITextField 中的日期格式

ios - 如何在 swift 4 中正确使用选择器

ios - 从模态 UIViewController 呈现 UIImagePickerController

ios - UIView 线性动画移动 View

ios - UIActionSheet 未显示在弹出窗口中

ios - UIActionSheet 不显示行分隔符

ios - 如何将 iOS 模拟器上的状态栏更改为 Apple 默认状态,如时间 : 9:41

ios - QuartzCore - CA::Render::Shmem::new_shmen() 内存泄漏

iphone - 委托(delegate)和执行SelectorOnMainThread

iphone - 无法检索同时触摸的数量