Donald Burr
Donald Burr

Reputation: 2281

(iOS/Objective-C) Better way of conditionally adding different buttons to a UIActionSheet?

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

Upvotes: 1

Views: 822

Answers (1)

larsacus
larsacus

Reputation: 7346

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")];

Upvotes: 4

Related Questions