ryryan
ryryan

Reputation: 3928

iOS iPad UIActionSheet Issue

I am currently developing an application which needs an option to 'share' using multiple services; such as email, twitter.

To to this, I have a UIBarButtonItem coded in and when touched, it triggers this:

UIActionSheet *sheet = [[UIActionSheet alloc] 
                            initWithTitle:@""
                            delegate:self
                            cancelButtonTitle:nil
                            destructiveButtonTitle:nil
                            otherButtonTitles:nil];

    [sheet addButtonWithTitle:@"Email"];
    [sheet addButtonWithTitle:@"Tweet"];
    [sheet addButtonWithTitle:@"Cancel"];

    sheet.cancelButtonIndex = sheet.numberOfButtons-1;

    [sheet showFromRect:self.view.bounds inView:self.view animated:YES];
    [sheet release];

In conjunction with this to detect which button is selected:

clickedButtonAtIndex:(NSInteger)buttonIndex {
    if (buttonIndex == actionSheet.cancelButtonIndex) { return; }
    switch (buttonIndex) {
        case 0:
        {

            [self emailThis];
            break;
        }
        case 1:
        {
            [self tweetThis];
            break;
        }
    }

This works a treat on the iPhone. But unfortunately it displays incorrectly on the iPad. It looks like it is trying to display the UIPopoverController, but it is positioned center of the navbar with practically no height.

I have looked into using the UIPopoverController, but I cannot seem to find out how to use it with buttons. Is there anyway I can adapt the code above to properly display the buttons, as it's trying to already.

Many thanks,

Ryan

PS: I'm new to objective-c/iOS coding, so please be specific. Thank you :)

EDIT: I have tried using:

[sheet showFromBarButtonItem:[self share] animated:YES];

But it doesn't work. Here is my code for the button:

UIBarButtonItem *share = [[UIBarButtonItem alloc] 
                                initWithTitle:@"Share"                                            
                                style:UIBarButtonItemStyleBordered 
                                target:self 
                                action:@selector(loadShare)];
    self.navigationItem.rightBarButtonItem = share;
    [share release];

Also here is the code in the .h file:

@interface DetailViewController : UIViewController <UITableViewDelegate, UITableViewDataSource> {
UIBarButtonItem *share;
}

@property (nonatomic, retain) IBOutlet UIBarButtonItem *share;

Here is the debug error:

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '* -[__NSPlaceholderArray initWithObjects:count:]: attempt to insert nil object from objects[0]'

Upvotes: 1

Views: 8686

Answers (3)

Warif Akhand Rishi
Warif Akhand Rishi

Reputation: 24248

self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAction target:self action:@selector(actionPhotoShare:)] autorelease];

...

-(void)actionPhotoShare:(id)sender
{
actionSheetShare = [[UIActionSheet alloc] initWithTitle:nil 
                                                              delegate:self 
                                                     cancelButtonTitle:nil
                                                destructiveButtonTitle:NSLocalizedString(@"ActionSheet_Cancel", @"")
                                                     otherButtonTitles:NSLocalizedString(@"ActionSheet_Email", @""),nil];

if (IS_DEVICE_IPAD) {
    [actionSheetShare showFromBarButtonItem:sender animated:YES];
}else {
    [actionSheetShare showInView:self.view];
}
}

Upvotes: 7

MarkPowell
MarkPowell

Reputation: 16540

Your issue is that you are using showFromRect with the rect being the entire screen. That means the popup is placing the "arrow" the best it can to center on the screen. What you want is either showFromBarButtonItem or pass in the rect (self.shareButtonItem.frame) of the button.

Upvotes: 0

melsam
melsam

Reputation: 4977

Instead of [sheet showFromRect], use [sheet showFromBarButtonItem], and point it to your Share UIBarButtonItem. Note: you may need to create an IBOutlet or ivar for your UIBarButtonItem if you don't have one already, but that's extremely simple and I'm guessing you can do that.

Upvotes: 0

Related Questions