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

发布于 2024-12-13 21:22:25 字数 2232 浏览 1 评论 0原文

在我的 iOS 应用程序中,我向用户呈现一个 UIActionSheet;然而,UIActionSheet 必须能够针对不同情况呈现不同的按钮(例如,如果用户没有运行 iOS 5,则 Twitter 支持不可用,因此不要显示“Tweet this”按钮;如果 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];
}

In my iOS app, I'm presenting a UIActionSheet to a user; however the UIActionSheet has to be able to present different buttons for different cases (e.g. if user is not running iOS 5, then Twitter support is unavailable, so don't show the "Tweet this" button; if AirPrint is unavailable, then user can't print, so don't show the "print" button, etc.) Right now I have this implemented in a really brain-dead fashion, basically using a bunch of if-then-else statements (see below). Is there a cleaner way of doing this?

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];
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

乖不如嘢 2024-12-20 21:22:25

这与使用 addButtonWithTitle:(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")];

This is about as simple as you're gonna get using addButtonWithTitle:(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")];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文