将NSMutableDictionary从一个视图传递到另一个视图

时间:2017-01-06 05:56:48

标签: ios objective-c copy nsmutabledictionary

我正在尝试将数据从一个ViewController传递到另一个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
}

请帮帮我。我做错了什么。

我在AppointmentDictionary = (null)收到viewwillappear。 虽然我正在做适当的复制。

5 个答案:

答案 0 :(得分:2)

试试这个:

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

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

答案 1 :(得分:2)

试试这个

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

答案 2 :(得分:1)

您正在创建InfoViewController实例而不是推送该视图控制器。你在这里做一个“performSegueWithIdentifier”新实例会创建,所以第一个实例“info”没有在这里传递。你可以用其他方式做。

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

选项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;
    }
}

在prepareforsegue方法中,您必须传递上述值。 注意:确保目标视图控制器是InfoViewController

答案 3 :(得分:1)

如果您使用| eid | cid | sid | event_type | period_start | period_end | eid | event_type | |---------|------------|------------------|------------|-------------------------|-----------------------------|---------|------------| | event_1 | customer_1 | subscription_456 | created | March, 11 2016 17:38:50 | September, 11 2016 18:38:50 | event_3 | deleted | 进行导航,则可以使用以下方法在ViewControllers之间传输数据

performsegue

答案 4 :(得分:1)

如果您正在使用performSegueWithIdentifier,那么在函数中提供正文。不要使用代码创建任何新实例并在故事板中提供segue -

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