gpt4 book ai didi

ios - Obj-c - 将数据传递到 detailview 时崩溃?

转载 作者:行者123 更新时间:2023-11-28 19:31:02 26 4
gpt4 key购买 nike

我正在使用以下代码将数据从我的 ViewController 传递到 detailViewController,但是无论我做什么,我的应用程序都会在该行崩溃

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

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

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSArrayI objectForKeyedSubscript:]: unrecognized selector sent to instance 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(细节 View )

- (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";

最佳答案

创建您自己的类来存储注释数据源的索引。这是 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;

关于ios - Obj-c - 将数据传递到 detailview 时崩溃?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44816917/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com