1

定数をインポートするすべてのクラスがそれらの値を使用できるように、iOS アプリの定数シングルトン クラス内でグローバル定数値を設定したいと考えています。

ただし、このアイデアで数時間遊んだ後でも、まだ機能させることができません。

私の Constants.m ファイルで

 @interface Constants()
 {
    @private
    int _NumBackgroundNetworkTasks;
    NSDateFormatter *_formatter;
 }
 @end

 @implementation Constants

 static Constants *constantSingleton = nil;
 //Categories of entries
 typedef enum
 {
   mapViewAccessoryButton = 999

  } UIBUTTON_TAG;


 +(id)getSingleton
 {

   .....
  }

定数シングルトンへの参照がある別のクラス MapViewController があり、このような列挙型にアクセスしようとしています

 myDetailButton.tag =  self.constSingleton.UIBUTTON_TAG.mapViewAccessoryButton;

ただし、これは機能していません。mapviewcontroller 内の UIBUTTON_TAG にアクセスできません

誰か提案はありますか?

ありがとう

4

2 に答える 2

3

アプリ全体で列挙型を使用できるようにする場合は、列挙型の定義を .m ファイルではなく .h ファイルに入れます。

更新

Objective-C は名前空間をサポートしておらず、クラス レベルの定数または列挙型もサポートしていません。

この線:

myDetailButton.tag =  self.constSingleton.UIBUTTON_TAG.mapViewAccessoryButton;

次のようにする必要があります。

myDetailButton.tag =  mapViewAccessoryButton;

UIBUTTON_TAGいくつかの .h ファイルで列挙型を定義すると仮定します。

Objective-C アプリをコンパイルするときは、すべての列挙型のすべての値に一意の名前を付ける必要があります。これは Objetive-C が C に基づいている結果です。

更新 2 :

列挙型ではなく、必要なものを取得する方法が 1 つあります。このようなものが動作するはずです:

定数.h:

@interface UIBUTTON_TAG_ENUM : NSObject

@property (nonatomic, readonly) int mapViewAccessoryButton;
// define any other "enum values" as additional properties

@end

@interface Constants : NSObject

@property (nonatomic, readonly) UIBUTTON_TAG_ENUM *UIBUTTON_TAG;

+ (id)getSingleton;

// anything else you want in Constants

@end

定数.m

@implementation UIBUTTON_TAG_ENUM

- (int)mapViewAccessoryButton {
    return 999;
}

@end

@implementation Constants {
    int _NumBackgroundNetworkTasks;
    NSDateFormatter *_formatter;
    UIBUTTON_TAG_ENUM *_uiButtonTag;
}

@synthesize UIBUTTON_TAG = _uiButtonTag;

- (id)init {
    self = [super init];
    if (self) {
        _uiButtonTag = [[UIBUTTON_TAG_ENUM alloc] init];
    }

    return self;
}

// all of your other code for Constants

@end

これで、次のことができます。

myDetailButton.tag =  self.constSingleton.UIBUTTON_TAG.mapViewAccessoryButton;

ただし、これにポイントがあるかどうかはわかりません。

于 2013-02-16T20:40:18.517 に答える
1

これを行う 1 つの方法は、列挙型をあまり変更しない場合は、プリコンパイル済みヘッダー (.pch) に貼り付けることです。

于 2013-02-16T20:34:25.627 に答える