(iOS / Objective-C) Лучший способ условно добавить разные кнопки в UIActionSheet? - PullRequest
1 голос
/ 07 ноября 2011

В моем приложении для 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];
}

1 Ответ

4 голосов
/ 07 ноября 2011

Это так же просто, как вы собираетесь использовать 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")];
...