4

アイテムを含む配列があり、これらを可変長メソッドに渡したいと思います。どうやってそれをしますか?

つまり、私はこれを持っています(例えば):

NSArray *array = [NSArray arrayWithObjects:@"1", @"2", @"3", nil];

[[UIAlertView alloc] initWithTitle:@"title" message:@"message" delegate:nil cancelButtonTitle:[array objectAtIndex:0] otherButtonTitles:[array objectAtIndex:1], [array objectAtIndex:2], nil];

ただし、配列には可変長のアイテムが含まれる可能性があるため、このようにハードコーディングすることはできません。

4

2 に答える 2

16

otherButtonTitlesのパラメータのドキュメントには、次のように-[UIAlertView initWithTitle:message:delegate:cancelButtonTitle:otherButtonTitles:]記載されています。

この引数を使用することは、このタイトルを使用してaddButtonWithTitle:を呼び出してボタンを追加することと同じです。

これを試しましたか:

NSArray *array = [NSArray arrayWithObjects:@"1", @"2", @"3", nil];
UIAlertView *view = [[UIAlertView alloc] initWithTitle:@"title" message:@"message" delegate:nil cancelButtonTitle:@"cancel" otherButtonTitles:nil];
for (NSString *s in array) {
    [view addButtonWithTitle:s];
}
于 2010-03-11T14:41:32.433 に答える
4
- (id) initWithTitle:(NSString *)title message:(NSString *)message delegate:(id)delegate cancelButtonTitle:(NSString *)cancelButtonTitle otherButtonTitles:(NSString *)otherButtonTitles ...
{
    va_list args;
    va_start(args, otherButtonTitles);
    for (NSString *arg = otherButtonTitles; arg != nil; arg = va_arg(args, NSString*))
    {
        //do something with nsstring
    }
    va_end(args);
}

配列を受け入れる関数で引数を作成することもできます(簡単な解決策)

とにかく...表記は、関数の最後にある可変量の引数用です。

于 2010-03-11T14:47:37.727 に答える