Wednesday, May 29, 2013

UIActionSheet showFromBarButtonItem animated example in Objective C (iOS).

UIActionSheet showFromBarButtonItem animated

Displays an action sheet that originates from the specified bar button item.

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

Parameters of [UIActionSheet showFromBarButtonItem animated]
item
The bar button item from which the action sheet originates.
animated
Specify YES to animate the presentation of the action sheet or NO to present it immediately without any animation effects.

Discussion of [UIActionSheet showFromBarButtonItem animated]
On iPad, this method presents the action sheet in a popover and adds the toolbar that owns the button to the popover’s list of passthrough views. Thus, taps in the toolbar result in the action methods of the corresponding toolbar items being called. If you want the popover to be dismissed when a different toolbar item is tapped, you must implement that behavior in your action handler methods.

UIActionSheet showFromBarButtonItem animated example.
-(IBAction) showMoreTools:(id)sender {
    // currently displaying actionsheet?
    if (actionSheet_) {
        [actionSheet_ dismissWithClickedButtonIndex:-1 animated:YES];
        actionSheet_ = nil;
        return;
    }

    actionSheet_ = [[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:nil destructiveButtonTitle:@"Close" otherButtonTitles:@"Add Bookmark", @"Add to Home Screen", @"Print", @"Share", nil];
    actionSheet_.actionSheetStyle = UIActionSheetStyleDefault;
    [popupQuery showFromBarButtonItem:moreTools animated:YES];
    [actionSheet_ release];  // yes, release it. we don't retain it and don't need to
}

Example of [UIActionSheet showFromBarButtonItem animated].
-(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];
}
}

UIActionSheet showFromBarButtonItem animated example.
- (void)promptResetDefaults:(id)sender
{
    UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:nil
                                                             delegate:self
                                                    cancelButtonTitle:nil
                                               destructiveButtonTitle:NSLocalizedString(@"Reset All Defaults", nil)
                                                    otherButtonTitles:nil];

    self.navigationController.navigationBar.userInteractionEnabled = NO;
    [actionSheet showFromBarButtonItem:sender animated:YES];
}

End of UIActionSheet showFromBarButtonItem animated example article.