创建UIActionSheet

58

我想要创建这种菜单,当然还有其他的菜单按钮。是否有默认的视图控制器可以代表它,或者我需要获取图片并自己创建呢。

在此输入图片描述


3
这个问题的标题和受欢迎程度要比重复的那个好得多。如果能重新开放这个问题,让人们添加更新后的答案就太好了。 - Suragch
2
请投票以重新开放。 - Stunner
@Suragch 你说得对。这比其他链接更有帮助和易懂。 - Soorej Babu
4个回答

169

您需要使用 UIActionSheet

首先,您需要在您的 ViewController.h 文件中添加 UIActionSheetDelegate

然后,您可以使用以下代码引用 actionsheet:

  UIActionSheet *popup = [[UIActionSheet alloc] initWithTitle:@"Select Sharing option:" delegate:self cancelButtonTitle:@"Cancel" destructiveButtonTitle:nil otherButtonTitles:
                        @"Share on Facebook",
                        @"Share on Twitter",
                        @"Share via E-mail",
                        @"Save to Camera Roll",
                        @"Rate this App",
                        nil];
   popup.tag = 1;
  [popup showInView:self.view];

那么你必须处理每个调用。

- (void)actionSheet:(UIActionSheet *)popup clickedButtonAtIndex:(NSInteger)buttonIndex {

  switch (popup.tag) {
    case 1: {
        switch (buttonIndex) {
            case 0:
                [self FBShare];
                break;
            case 1:
                [self TwitterShare];
                break;
            case 2:
                [self emailContent];
                break;
            case 3:
                [self saveContent];
                break;
            case 4:
                [self rateAppYes];
                break;
            default:
                break;
        }
        break;
    }
    default:
        break;
 }
}

这已经被弃用于iOS 8.x https://developer.apple.com/documentation/uikit/uialertcontroller#//apple_ref/occ/cl/UIAlertController


5
因为你是唯一一个称之为 UIActionSheet 并且提供了好的答案,所以我要给你个“+1”。 - rmaddy
1
我正在开发我的应用程序时看到了这个问题!它来自于我的代码,一字不差 ;) - apollosoftware.org
1
它也可以在不添加UIActionSheetDelegate的情况下工作...我想知道如何... - Zar E Ahmer
2
随着iOS 8的推出,现在有了一个新的UIAlertController。https://developer.apple.com/library/ios/documentation/uikit/reference/UIAlertController_class/index.html#//apple_ref/occ/cl/UIAlertController - apollosoftware.org
关于2014年9月24日的评论和编辑,现在有一个新的UIAlertController,而UIActionSheet已经在iOS 8中被弃用了,但是说它已经“改变”并不完全准确。最好包括类参考链接,但说它已被弃用可能更好些,而不是说它已经改变。 - Tony Adams
显示剩余2条评论

11

Take a look at the UIActionSheet documentation.

NSString *actionSheetTitle = @"Action Sheet Demo"; //Action Sheet Title
NSString *destructiveTitle = @"Destructive Button"; //Action Sheet Button Titles
NSString *other1 = @"Other Button 1";
NSString *other2 = @"Other Button 2";
NSString *other3 = @"Other Button 3";
NSString *cancelTitle = @"Cancel Button";
UIActionSheet *actionSheet = [[UIActionSheet alloc]
                              initWithTitle:actionSheetTitle
                              delegate:self
                              cancelButtonTitle:cancelTitle
                              destructiveButtonTitle:destructiveTitle
                              otherButtonTitles:other1, other2, other3, nil];
[actionSheet showInView:self.view];

5

这被称为UIActionSheet:您可以按以下方式创建:

    NSString *actionSheetTitle = @"Action Sheet Demo"; //Action Sheet Title
NSString *destructiveTitle = @"Destructive Button"; //Action Sheet Button Titles
NSString *other1 = @"Other Button 1";
NSString *other2 = @"Other Button 2";
NSString *other3 = @"Other Button 3";
NSString *cancelTitle = @"Cancel Button";
UIActionSheet *actionSheet = [[UIActionSheet alloc]
                              initWithTitle:actionSheetTitle
                              delegate:self
                              cancelButtonTitle:cancelTitle
                              destructiveButtonTitle:destructiveTitle
                              otherButtonTitles:other1, other2, other3, nil];
[actionSheet showInView:self.view];

实现UISctionSheetDelegate以响应按钮操作。

查看此教程以获取更多信息:http://mobile.tutsplus.com/tutorials/iphone/uiactionsheet_uiactionsheetdelegate(代码来自此教程)


3

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接