【问题标题】:Pass NSMutableDictionary from one view to another [duplicate]将 NSMutableDictionary 从一个视图传递到另一个视图
【发布时间】:2017-05-20 20:50:34
【问题描述】:

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

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

我在viewwillappear 中收到AppointmentDictionary = (null)。 虽然我正在做这个的正确副本。

【问题讨论】:

  • 这个问题已经被问了好几次了,你有没有看可用的 SO 帖子?
  • 这里有这么多错的地方,你怎么初始化vc是错的,而且你不需要初始化它来调用segue,你必须从prepareForSegue传递数据,最后是函数和属性名称应为小写
  • @Tj3n 感谢您的评论。我将更改属性和函数名称。必须从 prepareForSegue 传递数据?我不明白。
  • 查看这里stackoverflow.com/questions/7864371/…>

标签: ios objective-c copy nsmutabledictionary


【解决方案1】:

您正在创建 InfoViewController 实例而不是推送该视图控制器。您在此处执行“performSegueWithIdentifier”新实例将创建,因此您的第一个实例“信息”未通过此处。你可以用其他方式。

 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

【讨论】:

  • [[InfoViewController alloc] init]; 这将导致空白视图控制器,我认为宁愿让他使用 segue 更好
  • 更新了答案看看这个。
【解决方案2】:

试试这个:

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

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

【讨论】:

    【解决方案3】:

    如果您使用performsegue 进行导航,那么您可以使用以下方法在 ViewControllers 之间传输数据

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

    【讨论】:

      【解决方案4】:

      试试这个

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

      【讨论】:

        【解决方案5】:

        如果您使用 performSegueWithIdentifier,则在 function 中提供正文。不要使用代码创建任何新实例并在情节提要中提供 segue -

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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2015-10-23
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多