0

UIAlertView をサブクラス化しています。次のシグネチャを持つ init メソッドを実装したいと思います。

- (id)initWithTitle:(NSString *)title 
            message:(NSString *)message
         identifier:(NSInteger)ident
           delegate:(id)delegate 
  cancelButtonTitle:(NSString *)cancelButtonTitle 
  otherButtonTitles:(NSString *)otherButtonTitles,...

これは、 param が追加されたデフォルトの UIAlertView メソッドですidentifier

- (id)initWithTitle:(NSString *)title 
            message:(NSString *)message
           delegate:(id)delegate 
  cancelButtonTitle:(NSString *)cancelButtonTitle 
  otherButtonTitles:(NSString *)otherButtonTitles,...

コンパイル時に init メソッドのパラメーターが分からない場合、スーパー メソッドを呼び出すための正しい構文は何otherButtonTitlesですか?

self = [super initWithTitle:title
                    message:message
                   delegate:delegate
          cancelButtonTitle:cancelButtonTitle 
          otherButtonTitles:otherButtonTitles, nil];
//I will only get the first param with this syntax
4

2 に答える 2

1

まず、variadic functionsについて学びます。それはあなたを助けることができます: Wikipedia

Example:
#include <stdarg.h>

void logObjects(id firstObject, ...) // <-- there is always at least one arg, "nil", so this is valid, even for "empty" list
{
  va_list args;
  va_start(args, firstObject);
  id obj;
  for (obj = firstObject; obj != nil; obj = va_arg(args, id)) // we assume that all arguments are objects
    NSLog(@"%@", obj);
  va_end(args);
}

第二に、サブクラス UIAlertView ではなくObjectve-C カテゴリを作成する方が良い

Example:
@interface UIAlertView(Identifiers)
- (id)initWithTitle:(NSString *)title 
            message:(NSString *)message
         identifier:(NSInteger)ident
           delegate:(id)delegate 
  cancelButtonTitle:(NSString *)cancelButtonTitle 
  otherButtonTitles:(NSString *)otherButtonTitles,...;
@end
于 2012-05-28T10:48:16.880 に答える
0

ドキュメントは言う

サブクラス化に関する注意事項
UIAlertView クラスはそのまま使用することを意図しており、サブクラス化はサポートしていません。このクラスのビュー階層は非公開であり、変更してはなりません。

しかし、私はあなたができるとは思わないところまで。init通常のメソッドを実装し、渡された引数を使用してオブジェクトを構成することで、この便利なメソッドをいつでも再作成できます。

于 2012-05-28T10:48:06.333 に答える