Wednesday, May 29, 2013

UIActionSheet showFromToolbar example in Objective C (iOS).

UIActionSheet showFromToolbar

Displays an action sheet that originates from the specified toolbar.

- (void)showFromToolbar:(UIToolbar *)view

Parameters of [UIActionSheet showFromToolbar]
view
The toolbar from which the action sheet originates.

Discussion of [UIActionSheet showFromToolbar]
The appearance of the action sheet is animated.

On iPad, this method centers the action sheet in the middle of the screen. Generally, if you want to present an action sheet relative to a toolbar in an iPad application, you should use the showFromBarButtonItem:animated: method instead.

UIActionSheet showFromToolbar example.
-(IBAction)buttonsheet {

UIActionSheet *alertView = [[UIActionSheet alloc]
   initWithTitle: @\"File Attributes\"
   delegate:self
   cancelButtonTitle:@\"Cancel\"
   otherButtonTitles:@\"Rename File\", @\"Email File\", nil];
[alertView showFromToolbar: myToolBar];
}

Example of [UIActionSheet showFromToolbar].
//setup an action sheet
UIActionSheet *actionSheet = [[UIActionSheet alloc]
initWithTitle:@"Please make a choice"
delegate:self cancelButtonTitle:@"Cancel" destructiveButtonTitle:nil
otherButtonTitles:@"Do Something", nil];
[actionSheet setActionSheetStyle:UIActionSheetStyleBlackOpaque];

//appDelegate
your app delegate name *appDelegate = (your app delegate name *)[[UIApplication sharedApplication] delegate];

//is this a tabbed app?
if([appDelegate.rootApp.tabs count] > 0){
[actionSheet showFromTabBar:[appDelegate.rootApp.rootTabBarController tabBar]];
}else{
if(self.mapToolbar != nil){
[actionSheet showFromToolbar:self.mapToolbar];
}else{
[actionSheet showInView:[self view]];
}
}
[actionSheet release];

UIActionSheet showFromToolbar example.
UIActionSheet *act = [[UIActionSheet alloc] init];
    act.delegate = self;
  
    for (NSString* menu in _menus) {
        [act addButtonWithTitle:menu];
    }
  
    [act addButtonWithTitle:@"Cancel";
    act.cancelButtonIndex = [_menus count];
  
    [act showFromToolbar:self.toolbar];
    [act release];

End of UIActionSheet showFromToolbar example article.