简体   繁体   中英

How to pass NSMutableDictionary from one viewController to another View controller?

firstviewcontroller.m

@property (nonatomic) NSMutableDictionary *json;

.h
@synthesis json;

    viewdidload()

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

secondviewcontroller.h

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

FVC.json = @"My NSMutableDictionary"

This is my code, in firstviewcontrol NSMutableDictionary value is null. How to solve this?

Write in your "secondViewConroller.h" file

@property (nonatomic, Strong) NSMutableDictionary *json;

And pass it as in "firtstViewConroller.m"

NSMutableDictionary *temJSONDic = ....

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

Use below code to load your xib file using [NSBundle mainBundle] approach and the first object in returned array is your controller. Then assign your dictionary object and then push using 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];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM