1

プロパティにUIActionSheet値が含まれているかどうかに応じてプログラムで追加されるボタンを含む があります。

これは私が現在使用しているコードです:

- (IBAction)infoButtonTap:(id)sender {
    MyObject *obj = (MyObject *)[self.dataSource objectAtIndex:self.pageControl.currentPage];

    if (obj != nil) {
        UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:@"Details"
                                                                 delegate:self
                                                        cancelButtonTitle:nil
                                                   destructiveButtonTitle:nil
                                                        otherButtonTitles:nil];

        // Programatically add Other button titles if they exist.
        if (obj.firstProperty) {
            [actionSheet addButtonWithTitle:@"Dosomething"];
        }

        if (obj.secondProperty) {
            [actionSheet addButtonWithTitle:@"Dosomethingelse"];
        }

        [actionSheet addButtonWithTitle:@"Static button"];

        actionSheet.cancelButtonIndex = [actionSheet addButtonWithTitle:@"Cancel"];

        [actionSheet showInView:self.view.window];
        [actionSheet release];
    }
}

- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
    MyObject *obj = (MyObject *)[self.dataSource objectAtIndex:self.pageControl.currentPage];

    switch (buttonIndex) {
        case 0: // Dosomething
            break;
        case 1: // Dosomethingelse
            break;
        case 2: // Static button
            break;
        default:
            break;
    }
}

問題は、プロパティの 1 つが null である可能性があるという事実をどのように説明し、それを .xml 内で処理するclickedButtonAtIndex:かです。指標値が変わります。プロパティに何らかの値が含まれているかどうかに応じて、それぞれが表示される約 4 つまたは 5 つの異なるボタンがあります。

4

1 に答える 1

4

最も簡単な方法は、 のbuttonTitleAtIndex:メソッドを使用UIActionSheetしてボタンのタイトルを取得し、これを使用して実行するアクションを決定することです。

または、ボタンを追加する場所と同様のロジックを使用して、インデックスの意味を正確に把握することもできます。

于 2011-03-24T04:49:38.687 に答える