如何将NSMutableDictionary从一个viewController传递到另一个View控制器?

时间:2017-07-27 05:05:05

标签: ios objective-c nsmutabledictionary

firstviewcontroller.m

@property (nonatomic) NSMutableDictionary *json;

.h
@synthesis json;

    viewdidload()

    {
        nslog(@"%@",json);
    }

secondviewcontroller.h

firstviewcontroller *FVC = [[firstviewcontroller alloc]init];

FVC.json = @"My NSMutableDictionary"

这是我的代码,在firstviewcontrol NSMutableDictionary中,值为null。怎么解决这个问题?

2 个答案:

答案 0 :(得分:1)

写入"secondViewConroller.h"文件

@property (nonatomic, Strong) NSMutableDictionary *json;

并按照"firtstViewConroller.m"

传递
NSMutableDictionary *temJSONDic = ....

secondViewConroller *FVC = [[secondViewConroller alloc] initWithNibName:@"secondViewConroller" bundle:nil];
FVC.json = temJSONDic;
[self.navigationController pushViewController:FVC animated:YES];

答案 1 :(得分:1)

使用以下代码使用[NSBundle mainBundle]方法加载xib文件,返回数组中的第一个对象是控制器。然后分配您的字典对象,然后使用navigationcontroller推送。

    NSArray * array = [[NSBundle mainBundle] loadNibNamed:@"<your xib file name without .xib extension>" owner:self options:nil];
secondViewConroller *cntrl = [array objectAtIndex:0];
cntrl.json = <your dictionary data>;
[self.navigationController pushViewController:cntrl animated:YES];