views:

2948

answers:

4

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.

+2  A: 

I don't think that's feasible. I was sitting with the same problem.

Use

- (void)showFromRect:(CGRect)rect inView:(UIView *)view animated:(BOOL)animated

Example:

[actionSheet showFromRect:[[[myToolBar subviews] objectAtIndex:0] frame] inView:myToolBar animated:YES];

Note: You must change the index for objectAtIndex: to fit your situation and have reference to your ToolBar

stalinkay
Does that work through changes of orientation? Or do you need to adjust on rotation events?Thanks.
westsider
+2  A: 

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

- (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.

stalinkay
Stalinkay, thanks. What you describe is close to what I am doing now.I should have been more specific. I have my action sheet shown from a button in a toolbar at the bottom of screen. I also have a navigation bar across the top of the screen. I would like buttons in the nav bar to behave the same (with respect to action sheet) as buttons in toolbar. In other words, I would like nav bar buttons to be on action sheet's popover's passthrough list - and to have taps in nav bar buttons *both* dismiss action sheet *and* handle button event.This is why getting pointer to popover is important.
westsider
+2  A: 

I doubt there's a sanctioned approach to this since actionsheets are presented in UIPopoverViews which is linked to a UIViewController not a UIPopoverController

NSLog(@"%@",[[[popoverActionsheet superview] superview] passthroughViews]);

Looking through the UIPopoverView header file (UNDOCUMENTED) yields the following solution albeit undocumented. Doubt you can snick that past the App Reviewers but give it a go if you think it's worth a shot.

http://github.com/kennytm/iphone-private-frameworks/blob/master/UIKit/UIPopoverView.h

NSArray *passthroughViews = [[[popoverActionsheet superview] superview] passthroughViews];

        NSMutableArray *views = [passthroughViews mutableCopy];
        [views addObject:[self view]];
        [[[popoverActionsheet superview] superview] setPassthroughViews:views];
        [views release];
stalinkay
I think that this is what I was looking for - at some level. It is, obviously, fragile to the extent that Apple could change the underlying implementation by, say, adding another layer of views, etc.I am tempted to write my own version with popover. When I get around to that, I will post code here.Thanks, again!
westsider
Great. Would like to see what you come up with.
stalinkay