访问另一个ViewController的NSDictionary显示为null

时间:2014-01-14 06:23:14

标签: ios objective-c uiviewcontroller nsdictionary

我正在使用NSDictionary在地图上绘制各种标记。在绘制了引脚后,我点击标记的呼叫以显示用户详细信息。现在当我按下呼叫时,我正在传递详细信息像marker.title和marker.snippet到另一个View Controller。对这些值的引用我试图从字典中获取其他细节。这是我从另一个视图控制器访问字典时所做的。

  -(void)fetchMarkerData:(NSString *)typeOfMarker nameOfUser:(NSString *)username
 {
   NSString *type = typeOfMarker;
   NSString *name = username;
   NSLog(@"User Type - %@ & Name - %@ ", type, name); // shows name and type

  NSLog(@"members dict from map view - %@" ,  self.membersDetailsDict);  // shows null & self.membersDetailsDict is allocated and initialised in viewDidLoad.
  if ([type isEqualToString:@"Member"])
 {
    for (NSDictionary *instance in self.membersDetailsDict)
    {
        NSString *name= [instance objectForKey:@"pseudo"];
        if ([name isEqualToString:name])
        {
            self.nameLabel.text = name;
            self.displayPicture.image =[instance objectForKey:@"avatar"];
            self.cityLabel.text = [instance objectForKey:@"pays"];

        }
    }
}

}

以这种方式传递数据: -

    - (void)mapView:(GMSMapView *)mapView didTapInfoWindowOfMarker:(GMSMarker *)marker
    {
    UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:@"MainStoryboard"
                                                         bundle: nil];
    UserProfileViewController *userProfileViewController = (UserProfileViewController *)[mainStoryboard instantiateViewControllerWithIdentifier:@"UserProfileViewController"];


       [userProfileViewController fetchMarkerData:marker.snippet nameOfUser:marker.title];

       userProfileViewController.membersDetailsDict  = self.membersDict; //passing dictionary also          

       UINavigationController *nv = [[UINavigationController alloc]initWithRootViewController:userProfileViewController];
       [self.revealSideViewController popViewControllerWithNewCenterController:nv animated:YES];

1 个答案:

答案 0 :(得分:0)

似乎您正试图从第二个视图控制器中访问membersDict变量,而您实际上再次实例化了该变量,而您应该从最初创建它的位置访问实际的membersDict变量 - 我认为是在ViewController1。现在根据我的理解,这是你的设置。

  1. Viewcontroller1包含存储在名为membersDict
  2. 的变量中的地图数据点
  3. 用户点击某个点,它会推送viewController2,这就是您尝试从membersDict访问viewController1变量的位置。
  4. 您可以通过向ViewController2提供来自ViewController1的变量来实现此目的,而不是尝试从您的`ViewController2中访问membersDict被推了。

    所以你可以这样做,这只是一个粗略的例子:

    // in .h viewController file
    NSMutabledictionary *membersDict; //whatever the data object was that you stored the data in
    
    //in .m viewController1 file
    //viewDidLoad method
    membersDict = [[NSMutableDictionary alloc] init];
    //add whatever data you like to your membersDict
    
    //In section of code where youre about to push your second ViewController 2: Pass the members dict data too along with pushing the second viewController.
    
    ViewController2 = [[ViewController2 alloc] initWithWhatever];
    viewController2.membersDict = self.membersDict; //
    
    or
    ViewController2 = [[ViewController2 alloc] initWithMembersDict:self.membersDict];
    
    //Then push the viewController2.
    

    这将确保您的membersDict数据成功传递。