(iOS/Objective-C)向UIActionSheet有条件地添加不同按钮的更好方法?

(iOS/Objective-C)向UIActionSheet有条件地添加不同按钮的更好方法?,objective-c,ios,uiactionsheet,Objective C,Ios,Uiactionsheet,在我的iOS应用程序中,我向用户展示UIActionSheet;但是,UIActionSheet必须能够针对不同的情况显示不同的按钮(例如,如果用户未运行iOS 5,则推特支持不可用,因此不要显示“推特此”按钮;如果AirPrint不可用,则用户无法打印,因此不要显示“打印”按钮,等等)现在我已经以一种真正的死脑筋的方式实现了它,基本上使用了一堆if-then-else语句(见下文)。有没有更干净的方法 if(NSClassFromString(@"TWTweetComposeViewContr

在我的iOS应用程序中,我向用户展示UIActionSheet;但是,UIActionSheet必须能够针对不同的情况显示不同的按钮(例如,如果用户未运行iOS 5,则推特支持不可用,因此不要显示“推特此”按钮;如果AirPrint不可用,则用户无法打印,因此不要显示“打印”按钮,等等)现在我已经以一种真正的死脑筋的方式实现了它,基本上使用了一堆if-then-else语句(见下文)。有没有更干净的方法

if(NSClassFromString(@"TWTweetComposeViewController"))  {
    if ([TWTweetComposeViewController canSendTweet]) {
        actionSheet = [[UIActionSheet alloc] initWithTitle:NSLocalizedString(@"Options", @"Options string")
                                                             delegate:self
                                         cancelButtonTitle:NSLocalizedString(@"Cancel", @"Cancel button")
                                               destructiveButtonTitle:nil
                                         otherButtonTitles:NSLocalizedString(@"Open in Safari", @"Open in Safari button"), NSLocalizedString(@"E-mail to a Friend", @"E-mail to a Friend button"), NSLocalizedString(@"Print", @"Print button"), NSLocalizedString(@"Tweet This", @"Tweet This button"), nil];
    } else {
        actionSheet = [[UIActionSheet alloc] initWithTitle:NSLocalizedString(@"Options", @"Options string")
                                                  delegate:self
                                         cancelButtonTitle:NSLocalizedString(@"Cancel", @"Cancel button")
                                    destructiveButtonTitle:nil
                                         otherButtonTitles:NSLocalizedString(@"Open in Safari", @"Open in Safari button"), NSLocalizedString(@"E-mail to a Friend", @"E-mail to a Friend button"), NSLocalizedString(@"Print", @"Print button"), nil];
    }
} else {
    actionSheet = [[UIActionSheet alloc] initWithTitle:NSLocalizedString(@"Options", @"Options string")
                                              delegate:self
                                     cancelButtonTitle:NSLocalizedString(@"Cancel", @"Cancel button")
                                destructiveButtonTitle:nil
                                     otherButtonTitles:NSLocalizedString(@"Open in Safari", @"Open in Safari button"), NSLocalizedString(@"E-mail to a Friend", @"E-mail to a Friend button"), NSLocalizedString(@"Print", @"Print button"), nil];
}

这和使用
addbuttonwhithtitle:(NSString*)title
得到的一样简单:

actionSheet = [[UIActionSheet alloc] initWithTitle:NSLocalizedString(@"Options", @"Options string")
                                          delegate:self
                                 cancelButtonTitle:NSLocalizedString(@"Cancel", @"Cancel button")
                            destructiveButtonTitle:nil
                                 otherButtonTitles:NSLocalizedString(@"Open in Safari", @"Open in Safari button"), NSLocalizedString(@"E-mail to a Friend", @"E-mail to a Friend button"), NSLocalizedString(@"Print", @"Print button"), nil];

if(NSClassFromString(@"TWTweetComposeViewController"))
    if ([TWTweetComposeViewController canSendTweet])
        [actionSheet addButtonWithTitle:NSLocalizedString(@"Tweet This", @"Tweet This button")];

这和使用
addbuttonwhithtitle:(NSString*)title
得到的一样简单:

actionSheet = [[UIActionSheet alloc] initWithTitle:NSLocalizedString(@"Options", @"Options string")
                                          delegate:self
                                 cancelButtonTitle:NSLocalizedString(@"Cancel", @"Cancel button")
                            destructiveButtonTitle:nil
                                 otherButtonTitles:NSLocalizedString(@"Open in Safari", @"Open in Safari button"), NSLocalizedString(@"E-mail to a Friend", @"E-mail to a Friend button"), NSLocalizedString(@"Print", @"Print button"), nil];

if(NSClassFromString(@"TWTweetComposeViewController"))
    if ([TWTweetComposeViewController canSendTweet])
        [actionSheet addButtonWithTitle:NSLocalizedString(@"Tweet This", @"Tweet This button")];

这比以前好多了。:)谢谢只需确保添加额外按钮后,按钮索引仍然是您所期望的。这非常容易
addbuttonwhithtitle
返回一个
NSInteger
,它是刚刚添加的按钮的索引。因此,您可以执行类似于
myButtonIndex=[myActionSheet addButtonWithTitle:@“Some Title]”的操作;随后在委托方法中,您可以测试index==myButtonIndex
,等等。非常好-我没有意识到addButtonWithTitle返回了一个NSInteger。也许我应该更仔细地阅读我的文档-在iOS 7中,这会导致在“取消”之后添加新选项。这比我以前使用的要好得多。:)谢谢只需确保添加额外按钮后,按钮索引仍然是您所期望的。这非常容易
addbuttonwhithtitle
返回一个
NSInteger
,它是刚刚添加的按钮的索引。因此,您可以执行类似于
myButtonIndex=[myActionSheet addButtonWithTitle:@“Some Title]”的操作;随后在委托方法中,您可以测试index==myButtonIndex
,等等。非常好-我没有意识到addButtonWithTitle返回了一个NSInteger。也许我应该更仔细地阅读我的文档-在iOS 7中,这会导致在“取消”之后添加新选项。