开发者

How do I present alternative options in an iPhone UI?

I often see something like the bottom picture in iphone apps and was wondering if there is a standard way to achieve that.

To be clear, it is a view that covers half the screen, usually with buttons to choose different options. When I ask for standard way,开发者_StackOverflow中文版 I meant UITableView, UIAlertView, UIScrollView, etc...

How do I present alternative options in an iPhone UI?


You are looking for UIActionSheet: http://developer.apple.com/library/ios/documentation/uikit/reference/UIActionSheet_Class/Reference/Reference.html


Before using these (or any other UI element) also check what the iOS Human Interface Guidelines say about how you should use them. I often see action sheets abused, sadly enough.


Yes, it is called UIActionSheet. Here's an example:

        UIActionSheet *takeAction = [[UIActionSheet alloc] initWithTitle:@"" 
                                                                delegate:self 
                                                       cancelButtonTitle:@"Cancel" 
                                                  destructiveButtonTitle:@"Delete"
                                                       otherButtonTitles:@"New...", @"Open...", @"Save As...", nil];        
        [takeAction setActionSheetStyle:UIActionSheetStyleDefault];
// use this to implement it with a tabBarController; similar ways to work with navBars
        [takeAction showFromTabBar:(UITabBar *)[[self tabBarController] view]];
        [takeAction release];

As with an alertView, you respond to buttons with

- (void)actionSheet:(UIActionSheet *)actionSheet 

    clickedButtonAtIndex:(NSInteger)buttonIndex
    {
        if (buttonIndex == [actionSheet destructiveButtonIndex]) {
            [self deleteIt];
        } else if (buttonIndex == [actionSheet cancelButtonIndex]) {
        } else if (buttonIndex == 1) {
            [self createNew];
        } else if (buttonIndex == 2) {
            [self openSaved];
        } else if (buttonIndex == 3) {
            [self saveAs];
        } else {
        }
    }

Also, make sure to declare the viewController to be the delegate:

<UIActionSheetDelegate>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜