Wednesday, May 29, 2013

UIActionSheet showFromBarButtonItem example in Objective C (iOS).

UIActionSheet showFromBarButtonItem

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

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

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

Discussion of [UIActionSheet showFromBarButtonItem]
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 example.
-(IBAction) showMoreTools:(id)sender {
    // currently displaying actionsheet?
    if (actionSheet_) {
        [actionSheet_ dismissWithClickedButtonIndex:-1 animated:YES];
        actionSheet_ = nil;

    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].
actionSheetShare = [[UIActionSheet alloc] initWithTitle:nil
                                                destructiveButtonTitle:NSLocalizedString(@"ActionSheet_Cancel", @"")
                                                     otherButtonTitles:NSLocalizedString(@"ActionSheet_Email", @""),nil];

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

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

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

End of UIActionSheet showFromBarButtonItem example article.