Search code examples
iphonehyperlinkuibarbuttonitem

How to make UIBarButtonItem give menu-options?


You know how if you click and hold on a link in Safari (for iPhone obviously) it gives you a list of options like "open in new window", "Open". "Copy" etc?

How do you call this and is it possible to get a UIBarButtonItem to do this (but every time it is clicked not just when held down)?


Solution

  • This is how you show this menu:

    UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:nil
                                                             delegate:self 
                                                    cancelButtonTitle:@"Cancel", 
                                               destructiveButtonTitle:nil
                                                    otherButtonTitles:@"Button 1", @"Button 2", @"Button 3", nil];
    actionSheet.actionSheetStyle = UIActionSheetStyleDefault;
    
    [actionSheet showInView:[self view]];
    [actionSheet release];
    

    In order to connect it to UIBarButtonItem just point the bar button item to a selector that will include the code above.

    Don't forget to implement - (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex method of UIActionSheetDelegate.