0

我有一个名为 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];
}

在这里,我评估 NSDictionary:

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

    ...
}

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

我在哪里犯错?

4

2 回答 2

2

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

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

        ...
    }
于 2013-05-09T12:02:59.807 回答
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
于 2013-05-09T12:57:09.653 回答