1

この方法で iPad に UIActionSheet を表示しています。

_actionSheet = [[UIActionSheet alloc] initWithTitle:@"Choose action"
                                           delegate:(id<UIActionSheetDelegate>)self
                                  cancelButtonTitle:nil
                             destructiveButtonTitle:nil
                                  otherButtonTitles:nil];

if (special_case) {
    _actionSheet.destructiveButtonIndex = [_actionSheet addButtonWithTitle:@"Discard Action"];
}

[_actionSheet addButtonWithTitle:@"Action One"];
[_actionSheet addButtonWithTitle:@"Action Two"];
_actionSheet.cancelButtonIndex = [_actionSheet addButtonWithTitle:@"Cancel"];

[_actionSheet showFromBarButtonItem:self.myActionButton animated:YES];

両方とも正常cancelButtonIndexdestructiveButtonIndex動作しますが、奇妙なことは、actionSheet:didDismissWithButtonIndex: にfirstOtherButtonIndex設定されていることです。これは、ボタンのインデックスがであるのに対し、インデックスが である-1ため、明らかに間違っています。Action One1Action Two2

明らかに、上記の全体的なポイントは、特別な場合にのみ破壊的なボタンを表示することです (そうでなければ、init 呼び出しですべてのタイトルを渡し、おそらく物事はうまくいったでしょう)。

私は何が欠けていますか?

4

1 に答える 1

2

firstOtherButtonIndexおそらくinitWithTitleメソッドでのみ設定されます。ボタンを手動で追加する場合は、手動で設定する必要があります。この問題は、他のボタンを追加した後にキャンセル ボタンを追加することによっても発生する可能性があります。通常、破壊ボタンとキャンセル ボタンのインデックスは よりも低くなりfirstOtherButtonIndexます。

簡単にやってみたらどうですか?


_actionSheet = [[UIActionSheet alloc] initWithTitle:@"Choose action"
                                           delegate:(id)self
                                  cancelButtonTitle:@"Cancel"
                             destructiveButtonTitle:(special_case ? @"Discard" : nil)
                                  otherButtonTitles:@"Action One", @"Action Two", nil];
于 2012-07-23T08:35:25.957 に答える