Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Accessing UIPopoverController for UIActionSheet on iPad

On the iPad, one can show a UIActionSheet using -showFromBarButtonItem:animated:. This is convenient because it wraps a UIPopoverController around the action sheet and it points the popover's arrow to the UIBarButtonItem that is passed in.

However, this call adds the UIBarButtomItem's toolbar to the list of passthrough views - which isn't always desirable. And, without a pointer to the UIPopoverController, one can't add other views to the passthrough list.

Does anyone know of a sanctioned approach to getting a pointer to the popover controller?

Thanks in advance.

like image 728
westsider Avatar asked May 10 '10 22:05

westsider


People also ask

How do I view Actionsheet on iPad?

On iPad, UIKit requires that you display an action sheet inside a popover. The following image shows an action sheet anchored to a bar button item. To display your action sheet in a popover, specify your popover's anchor point using the popoverPresentationController property of your alert controller.

What is action sheet in iOS?

An action sheet is a modal view that presents choices related to an action people initiate. DEVELOPER NOTE When you use SwiftUI, you can enable action sheet functionality in all platforms by specifying a presentation modifier for a confirmation dialog.

Which class is used to show an Actionsheet in iOS?

Overview. In apps that target versions of iOS prior to iOS 8, use the UIActionSheet class to present the user with a set of alternatives for how to proceed with a given task. You can also use action sheets to prompt the user to confirm a potentially dangerous action.

What is action sheet in iOS Swift?

An action sheet is a specific style of alert that appears in response to a control or action, and presents a set of two or more choices related to the current context.


2 Answers

You would need to adjust on orientation change.

I have found an alternative solution, that works perfectly for me.

Stick to

- (void)showFromBarButtonItem:(UIBarButtonItem *)item animated:(BOOL)animated

In your @interface add

UIActionSheet *popoverActionsheet;

also add

@property (nonatomic, retain) UIActionSheet *popoverActionsheet;

also add

- (IBAction)barButtonItemAction:(id)sender;

So you have reference to your actionsheet from anywhere in your implementation.

in your implementation

- (IBAction) barButtonItemAction:(id)sender
{
    //If the actionsheet is visible it is dismissed, if it not visible a new one is created.
    if ([popoverActionsheet isVisible]) {
        [popoverActionsheet dismissWithClickedButtonIndex:[popoverActionsheet cancelButtonIndex] 
                                                     animated:YES];
        return;
    }

    popoverActionsheet = [[UIActionSheet alloc] initWithTitle:nil
                                                     delegate:self
                                            cancelButtonTitle:nil 
                                       destructiveButtonTitle:nil
                         otherButtonTitles:@"Save Page", @"View in Safari", nil];

    [popoverActionsheet showFromBarButtonItem:sender animated:YES];
}

in your actionsheet delegate implement

- (void)actionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger)buttonIndex
{

    if (buttonIndex == [actionSheet cancelButtonIndex]) return;

    //add rest of the code for other button indeces
}

and don't forget to release popoverActionsheet in

- (void)dealloc

I trust that this will do.

like image 138
stalinkay Avatar answered Sep 19 '22 12:09

stalinkay


    if ([[[[actionSheet superview] superview] nextResponder] respondsToSelector:@selector(setPassthroughViews:)]) {
        [[[[actionSheet superview] superview] nextResponder] performSelector:@selector(setPassthroughViews:) withObject:nil];
    }

Will do the trick, this shouldn't cause any problems with the App Reviewers either as its not calling any private APIs.

It is fragile - the if statements ensures your code won't crash (just not work) in the unlikely event Apple change the underlying implementation.

like image 21
nbransby Avatar answered Sep 18 '22 12:09

nbransby