Pass NSMutableDictionary from one view to another

285 Views Asked by At

I am trying to pass data from one ViewController to another ViewController.

Firstviewcontroller.m

-(void)NavigateToAnotherScreen:(NSMutableDictionary*)dict{
    [self performSegueWithIdentifier:@"NavDashBoardToInfo" sender:self];

}

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([[segue identifier] isEqualToString:@"NavDashBoardToInfo"])
    {
        InfoViewController *vc = [segue destinationViewController];
        [vc setAppointmentDictionary:dict];
    }
}

Secondviewcontroller.h

@property (strong, nonatomic) NSMutableDictionary *AppointmentDictionary;

Secondviewcontroller.m

@synthesize AppointmentDictionary;

- (void)viewWillAppear:(BOOL)animated{
    NSLog(@"dict===>%@",AppointmentDictionary); // This gives me null
}

Please help me. What i am doing wrong.

I am getting AppointmentDictionary = (null) in viewwillappear. Though I am doing proper copy of this.

5

There are 5 best solutions below

0
On BEST ANSWER

try this

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([segue.destinationViewController respondsToSelector:@selector(setAppointmentDictionary:)]) {
    [segue.destinationViewController performSelector:@selector(setAppointmentDictionary:)
                                          withObject:dict];
}
}
2
On

You are creating InfoViewController instance and not pushing that view controller. You doing a "performSegueWithIdentifier" here new instance will create, so you first instance "info" is not passing here. You can do in other way.

 InfoViewController *info = [[InfoViewController alloc] init];
 info.AppointmentDictionary = [dict mutableCopy];
 [self.navigationController pushViewController: info animated:true];

Option 2:

-(void)NavigateToAnotherScreen:(NSMutableDictionary*)dict{
    [self performSegueWithIdentifier:@"NavDashBoardToInfo" sender: [dict mutableCopy]];

}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    if ([segue.identifier isEqualToString:@"NavDashBoardToInfo"]) {
        InfoViewController *viewController = (InfoViewController *)segue.destinationViewController;
        viewController.AppointmentDictionary = (NSMutableDictionary *)sender;
    }
}

In the prepareforsegue method you have to pass the value like mentioned above. Note: make sure your destination view controller is InfoViewController

0
On

Try this one:

-(void)NavigateToAnotherScreen:(NSMutableDictionary*)dict
{
    [self performSegueWithIdentifier:@"NavDashBoardToInfo" sender:dict];
}

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
         InfoViewController *info = segue.destinationViewController;
         info.AppointmentDictionary = sender;
}
0
On

If you are using performsegue for navigation then you can use below method for transferring data between ViewControllers

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([[segue identifier] isEqualToString:@"NavDashBoardToInfo"])
    {
        InfoViewController *info = [segue destinationViewController];
        info.AppointmentDictionary = "Value you want to pass";
    }
}
0
On

If you are using performSegueWithIdentifier then provide the body in function . Don't create any new instance using code and provide segue in storyboard -

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    if ([segue.identifier isEqualToString:@"YourController_Segue"]) {
        YourController *viewController = (YourController *)segue.destinationViewController;
        viewController.AppointmentDictionary = (NSMutableDictionary *)sender;
    }
}