Obj-c - 将数据传递给detailview时发生崩溃?

时间:2017-06-29 05:23:02

标签: ios objective-c

我使用以下代码将数据从我的ViewController传递给detailViewController,但无论我做什么,我的应用程序都崩溃了

self.username.text = self.mapuserData[@"users_name"];

出现以下错误(即使self.mapuserData中存在数据)?救命啊!

  

由于未捕获的异常而终止应用   'NSInvalidArgumentException',原因:' - [__ NSArrayI   objectForKeyedSubscript:]:无法识别的选择器发送到实例   0x17126b840'

ViewController.m

  - (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view
    {
        UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self  action:@selector(calloutTapped:)];
        [view addGestureRecognizer:tapGesture];
    }

 -(void)calloutTapped:(UITapGestureRecognizer *) sender
        {
            UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];

            OtherUserViewController *yourViewController = (OtherUserViewController *)[storyboard instantiateViewControllerWithIdentifier:@"OtherUserViewController"];

           NSMutableDictionary *dictionary = [self.addressData mutableCopy];
        yourViewController.mapuserData = dictionary; // this is how you do it

           [self.navigationController pushViewController:yourViewController animated:YES];

        }

OtherViewController.m(detailview)

- (void)viewDidLoad {
    [super viewDidLoad];

    if ([self.mapuserData count] > 0) {
        NSLog(@"This is map user data %@", self.mapuserData);

        self.addFriend.hidden = NO;

      self.username.text = self.mapuserData[@"users_name"];

       self.userBio.text = self.mapuserData[@"userbio"];

       NSString *thirdLink = self.mapuserData[@"photo_path"];

      NSString *ImageURLTwo = thirdLink;
       NSData *imageDataTwo = [NSData dataWithContentsOfURL:[NSURL URLWithString:ImageURLTwo]];

        self.userPhoto.image = [[UIImage alloc] initWithData:imageDataTwo];


    }

}

以下是self.mapuserData:

中的内容
This is map user data ( 
{ 
address = "2957 fake street"; 
childrenunder = Yes; 
city = Vancouver; 
"emergency facility" = None; 
"first name" = josh; 
"last name" = tree; 
phone = 6046710890; 
"photo_path" = "http://url.ca/paw.png"; 
"points balance" = 24; 
"postal code" = b6b6v5; 
"profile photo" = "<null>"; 
"property type" = Apartment; 
province = bc; 
"special skills" = "Medication"; 
"star rating" = 0; 
"street_address" = none; 
supervision = Yes; 
uid = 182; 
userbio = nfkkdkckmfkekxkx; 
"users_name" = "josh_tree@hotmail.com";

3 个答案:

答案 0 :(得分:2)

创建自己的类来存储Annotation数据源的索引。哪个是MKPointAnnotation

的子类

<强> PointAnnotation.h

#import <UIKit/UIKit.h>
#import <MapKit/MapKit.h>

@interface PointAnnotation : MKPointAnnotation

@property (nonatomic, assign)int index;

@end

<强> PointAnnotation.m

#import "PointAnnotation.h"

@implementation PointAnnotation

@end

更改您的添加注释代码,如下所示

    int index = 0;   //Index to track the data source index while select the annotation call out view.

    for (NSMutableDictionary *multiplelocations in self.addressData) {

        NSString *location = multiplelocations[@"street_address"];
        NSLog(@"Pull addresses %@", location);
        NSString *userNames = multiplelocations[@"users_name"];
        NSString *userBio = multiplelocations[@"userbio"];


        CLGeocoder *geocoder = [[CLGeocoder alloc] init];

        [geocoder geocodeAddressString:location
                     completionHandler:^(NSArray* placemarks, NSError* error){
                         if (placemarks && placemarks.count > 0) {
                             CLPlacemark *topResult = [placemarks objectAtIndex:0];
                             MKPlacemark *placemark = [[MKPlacemark alloc] initWithPlacemark:topResult];

                             MKCoordinateRegion region = self.mapView.region;

                             region.span.longitudeDelta /= 8.0;
                             region.span.latitudeDelta /= 8.0;


                             PointAnnotation *point = [[PointAnnotation alloc] init];
                             point.coordinate = placemark.coordinate;
                             point.title = userNames;
                             point.subtitle = userBio;
                             point.index = index;  // Store index here.

                             [self.mapView addAnnotation:point];
                         }
                     }
         ];
        index = index + 1;
    }

并修改您的didSelectAnnotationView代码,如下所示

 - (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view {

        //Get the annotation object from callout view.
        PointAnnotation *selectedPoint = (PointAnnotation *) view.annotation;

        UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];

        OtherUserViewController *yourViewController = (OtherUserViewController *)[storyboard instantiateViewControllerWithIdentifier:@"OtherUserViewController"];

        //Get the dictionary from array by using the index of the custom        PointAnnoation object.
        NSMutableDictionary *dictionary = self.addressData[selectedPoint.index];
        yourViewController.mapuserData = dictionary; 

        [self.navigationController pushViewController:yourViewController animated:YES];
    }

您要将Array分配给Dictionary,这是错误的。

   NSMutableDictionary *dictionary = [self.addressData mutableCopy];
        yourViewController.mapuserData = dictionary;

答案 1 :(得分:1)

您期望的词典是运行时的数组类型,因此您需要更改上面的代码

 NSMutableDictionary *dictionary = [self.addressData[0] mutableCopy];

addressData更改为NSArray

答案 2 :(得分:0)

self.mapuserData不再反序列化为字典,它反序列化为数组。

所以你想要做的是:

从零索引表单self.mapuserData获取对象,然后另存为字典。

示例:NSDictionary *dict = self.mapuserData[0];

self.mapuserData应该是数组类型。