iOS NSDictionary传递函数

时间:2013-05-09 11:55:19

标签: ios function nsdictionary viewcontroller

我的视图控制器名为SecondViewController,当用户选择要阅读的文章时,我想导航到ThirdViewController并传递NSDictionary文章数据。

- (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];
}

我在这里向NSDicitionary致敬:

- (id)initWithArticlesData:(NSDictionary *)data
{
    self = [super initWithNibName:nil bundle:nil];
    if (self)
    {
        articles = data;

    ...
}

但是在用户交互文章变量之后调用的其他函数似乎不正确 - 它不包含我的数据。

我哪里弄错了?

2 个答案:

答案 0 :(得分:2)

制作字典副本,然后分配。

 - (id)initWithArticlesData:(NSDictionary *)data
    {
        self = [super initWithNibName:nil bundle:nil];
        if (self)
        {
            articles = [data copy];

        ...
    }

答案 1 :(得分:0)

  

第一个选项

使用此功能......

-(id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil articles:(NSDictionary *)article
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self)
    {
        self.articles = article;
    }
    return self;
}

推送使用这个......

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
  

第二个选项

只需使用此..

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