问题
I have my view controller named SecondViewController and when user selects which article wants to read, I want to navigate to ThirdViewController and pass NSDictionary with article data.
- (void) touchButtonBlock:(NSInteger) select
{
NSLog(@"touchButtonBlock");
NSDictionary *dict;
switch (select) {
case 0:
{
NSMutableDictionary *tmpDict = [[NSMutableDictionary alloc] init];
NSMutableDictionary *val = [[NSMutableDictionary alloc] init];
[val setObject:@"article360.html" forKey:@"page"];
[val setObject:@"img.png" forKey:@"img"];
[val setObject:@"art1" forKey:@"name"];
[tmpDict setObject:val forKey:[NSNumber numberWithInt:0]];
val = [[NSMutableDictionary alloc] init];
[val setObject:@"article360.html" forKey:@"page"];
[val setObject:@"img.png" forKey:@"img"];
[val setObject:@"art2" forKey:@"name"];
[tmpDict setObject:val forKey:[NSNumber numberWithInt:1]];
dict = [NSDictionary dictionaryWithDictionary:tmpDict];
//[tmpDict release];
//[val release];
break;
}
NSDictionary *immDict = [NSDictionary dictionaryWithDictionary:dict];
ArticleSelectListViewController *selectorView = [[ArticleSelectListViewController alloc] initWithArticlesData:immDict];
[self.navigationController pushViewController:selectorView animated:YES];
[selectorView release];
}
Here I assing NSDicitionary:
- (id)initWithArticlesData:(NSDictionary *)data
{
self = [super initWithNibName:nil bundle:nil];
if (self)
{
articles = data;
...
}
But in the other functions that are being called after user interaction articles variable seems to be incorrect - it doesn't contain my data.
Where do I make mistake ?
回答1:
Make a copy of dictionary and then assign it.
- (id)initWithArticlesData:(NSDictionary *)data
{
self = [super initWithNibName:nil bundle:nil];
if (self)
{
articles = [data copy];
...
}
回答2:
FIRST OPTION
Use this function...
-(id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil articles:(NSDictionary *)article
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self)
{
self.articles = article;
}
return self;
}
For push use this...
NSDictionary *immDict = [NSDictionary dictionaryWithDictionary:dict];
ArticleSelectListViewController *selectorView = [[ArticleSelectListViewController alloc] initWithNibName:@"ArticleSelectListViewController" bundle:nil articles:immDict];
[self.navigationController pushViewController:selectorView animated:YES];
[selectorView release];//FOR NON ARC
SECOND OPTION
Just use this..
NSDictionary *immDict = [NSDictionary dictionaryWithDictionary:dict];
ArticleSelectListViewController *selectorView = [[ArticleSelectListViewController alloc] initWithNibName:@"ArticleSelectListViewController" bundle:nil];
selectorView.articles = immDict;
[self.navigationController pushViewController:selectorView animated:YES];
[selectorView release];//FOR NON ARC
来源:https://stackoverflow.com/questions/16461189/ios-nsdictionary-pass-through-function