0

This question refers to the native iOS function of displaying actionsheets and popovers when a phone number or email is selected in a webview/textview from an iPad. In case you need a refresher, the actionsheet contains two buttons that read "Add to Contacts" and "Copy". Where the popover that I am referring to is what is displayed when the user selects the "Add to Contacts" button.

I understand that to dismiss a popover one declares:

[somePopoverController dismissPopoverAnimated:YES];

To dismiss an actionsheet, one can simply call:

[someActionSheet dismissWithClickedButtonIndex:0 animated:YES];

However, my problem is that since iOS created these actionsheets/popovers for me, I do not own them, and cannot reference them to call the dismiss methods.

The use case of why I need to do this is because my application will log the user out after a specified time of no activity. When the user is logged out these actionsheets/popovers still remain on the screen, even though I have entered a new view controller and removed the old ones.

Does anyone know of how I can reference these popovers and actionsheets that I do not own? Any advice would be greatly appreciated!

4

2 回答 2

1

好吧,我找到了一种解决方案,可以在 iPad 上检测电话号码/电子邮件时同时消除 UIActionSheets 和从 Webviews/Textviews 创建的弹出框。解决方案基本上是使用递归遍历我的所有视图,直到找到我想要关闭的视图。很多功劳归功于我在这篇文章Accessing UIPopoverController for UIActionSheet on iPad中找到的答案。

要删除 UIActionSheets:

BOOL IsActionOpen(UIView* aView) {
    BOOL    actionOpen = NO;
    if (aView) {
        if ([aView isKindOfClass:[UIActionSheet class]]) {
            actionOpen = YES;
            [(UIActionSheet *)aView dismissWithClickedButtonIndex:0 animated:NO];
        }
        else if (aView.subviews.count > 0) {
            for (UIView* aSubview in aView.subviews) {
                if ( IsActionOpen( aSubview)) {
                    if ([aView isKindOfClass:[UIActionSheet class]]) {
                        actionOpen = YES;
                        [(UIActionSheet *)aView dismissWithClickedButtonIndex:0 animated:NO];
                    }break;
                }
            }
        }
    }
    return actionOpen;
}

- (void) removeActionSheetIfShowing {
    BOOL    actionOpen = NO;
    for (UIWindow* w in [UIApplication sharedApplication].windows) {
        actionOpen =  IsActionOpen(w);
        if (actionOpen)
            break;
    }
}

要删除从生成的 UIActionSheet 中选择“添加到联系人”按钮时创建的弹出框:

BOOL IsPopOverOpen(UIView* aView) {
    BOOL    popOverOpen = NO;
    if (aView) {
        //popover is a popoverview, not a popovercontroller. We find it by checking if UIPopoverBackgroundView exists
        if([aView isKindOfClass:[UIPopoverBackgroundView class]]){
            popOverOpen = YES;
            [aView.superview removeFromSuperview];
        }
        else if (aView.subviews.count > 0) {
            for (UIView* aSubview in aView.subviews) {
                if ( IsPopOverOpen( aSubview)) {
                    if([aView isKindOfClass:[UIPopoverBackgroundView class]]){
                        popOverOpen = YES;
                        [aView.superview removeFromSuperview];
                    }break;
                }
            }
        }
    }
    return popOverOpen;
}

- (void) removePopOverIfShowing {
    BOOL    popOverOpen = NO;
    for (UIWindow* w in [UIApplication sharedApplication].windows) {
        popOverOpen =  IsPopOverOpen(w);
        if (popOverOpen)
            break;
    }
}
于 2013-10-02T16:32:52.300 回答
0

您所需要的只是在您的视图控制器中保留对它们的引用。UIActionSheet例如,当您创建 时,只需将其存储在类型的属性中UIActionSheet

@property (strong, nonatomic) UIActionSheet *myActionSheet;

然后,当您实际创建操作表时,您执行

self.myActionSheet = [[UIActionSheet alloc] initWithTitle:myTitle 
                                                 delegate:self 
                                        cancelButtonTitle:cancelButtonTitle 
                                   destructiveButtonTitle:destButtonTitle 
                                        otherButtonTitles:otherButtonTitle, nil];

[self.myActionSheet showFromBarButtonItem:sender animated:YES];

当你需要解雇它时,你只需做

[self.myActionSheet dismissWithClickedButtonIndex:0 animated:YES];
于 2013-09-30T15:45:25.320 回答