我正在使用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];
答案 0 :(得分:0)
似乎您正试图从第二个视图控制器中访问membersDict
变量,而您实际上再次实例化了该变量,而您应该从最初创建它的位置访问实际的membersDict
变量 - 我认为是在ViewController1
。现在根据我的理解,这是你的设置。
Viewcontroller1
包含存储在名为membersDict
。viewController2
,这就是您尝试从membersDict
访问viewController1
变量的位置。您可以通过向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数据成功传递。