複数のUITableView間でNSMutableDictionaryを共有したいと思います。重要なのは、1つのビューで、値として配列と対応するキーをディクショナリに追加し、SingletonObjectのディクショナリプロパティを設定できるようにすることです。次に、別のビューで、SingletonObjectのプロパティを介して辞書の配列にアクセスできます。
SingletonObjectの場合、ヘッダーファイルには次のものがあります。
@property(nonatomic) NSMutableDictionary * dict;
+(SingletonObject *) sharedManager;
SingletonObjectの実装ファイルには、次のものがあります。
@synthesize dict;
+(SingletonObject *)sharedManager {static SingletonObject * sharedResourcesObj = nil;
@synchronized(self)
{
if (!sharedResourcesObj)
{
sharedResourcesObj = [[SingletonObject alloc] init];
}
}
return sharedResourcesObj;
}
次に、UITTableViewクラスの1つで次のことを行います
// instantiate the SingletonObject
sharedResourcesObj = [SingletonObject sharedManager];
// instantiate array
NSMutableArray *courseDetails = courseDetails = [[NSMutableArray alloc]init];
// put textview value into temp string
NSString *tempString = tempString = [[NSString alloc]initWithString:[_txtBuildingRoom text]];
// put textview value into array (via temp string)
[courseDetails addObject:tempString];
// set dictionary property of SingletonObject
[sharedResourcesObj.dict setObject:courseDetails forKey:_lblCourse.text];
問題は、すべてを1行ずつコンソールに出力すると、辞書の新しい値がないことを除いて、すべてに値があり、正しく機能していることです。
以下のコードでディクショナリの値またはカウントを確認すると、カウントは0であり、ディクショナリにオブジェクトがありません。
// dictionary count
NSLog(@"%i", sharedResourcesObj.dict.count);
// get from dictionary
NSMutableArray *array = [sharedResourcesObj.dict objectForKey:_lblCourse.text];
// display what is in dictionary
for (id obj in array)
{
NSLog(@"obj: %@", obj);
}
UITableView間で辞書を共有するための正しい概念を使用していますか?
SingletonObjectの実装に問題はありますか?
以前、タブ間で整数値を共有するためにこのSingletonObjectの実装を使用しましたが、まったく問題はありませんでした。現在の唯一の違いは、SingletonObjectのプロパティが整数ではなく、NSMutableDictionaryであるということです。
誰か助けてもらえますか?