我最终成功获得了callOutAccecoryControlTapped-method中点击的注释索引。 :) 我创建了一个名为Annotation(Annnotation * ann)的自定义注释类,我在parse.com上填充了我的数据库中的值。 但现在我不知道如何将值传递给我的下一个视图LAOpeningHoursViewController? 我在调试时可以看到所有的值,而* ann(se代码)例如:ann.cname =" Olles cafe"等等。这很好。
我将整个ann-object传递给我的下一个视图。但我不知道如何将ann.cname放到我的自我.myLabel.text'上。我给你看了代码:
如果你有善意回答我,请解释得非常好,因为我是初学者。 最好的办法是,如果你在编写代码片段时遇到麻烦......;)
这是第一个视图的一些代码:
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation {
MKPinAnnotationView *pinView = (MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:@"pinView"];
if (!pinView) {
pinView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"pinView"];
pinView.pinColor = MKPinAnnotationColorRed;
pinView.animatesDrop = YES;
pinView.canShowCallout = YES;
UIButton *rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
pinView.rightCalloutAccessoryView = rightButton;
} else {
pinView.annotation = annotation;
}
return pinView;
}
- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control {
Annotation *ann = [[mapView selectedAnnotations] objectAtIndex:0];
NSLog(@"ann.subtitle = %@", ann.subtitle);
NSLog(@"ann.cid = %@", ann.cid);
NSLog(@"ann.cstr = %@", ann.cstr);
NSLog(@"ann.cname = %@", ann.cname);
//this works fine, I get all the values
[self performSegueWithIdentifier: @"openingHours" sender:ann];
}
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString:@"openingHours"]) {
LAOpeningHoursViewController *secondView = (LAOpeningHoursViewController*)segue.destinationViewController;
//PFObject *tempObject = [[myMapView selectedAnnotations] objectAtIndex:0];
Annotation *ann = (id)sender;
//Here is the problem: What to pass? I want to pass for example ann.cname that is declared in my custom annotation
//Both 'ann' and 'tempObject' has the right values for cname but I dont know how to pass it to the next view
secondView.myLabel.text = self.stringToPass;
//secondView.myLabel.text = ann.cname;
}
}
以下是第二个视图中名为LAOpeningHoursViewController的代码,它应该接收数据:
- (void)viewDidLoad
{
[super viewDidLoad];
self.myLabel.text = ann.cname;
//recieving an error-code… of course
}
在LAOpeningHoursViewController.h中声明为
@property (strong, nonatomic) IBOutlet UILabel *myLabel;
@property (strong, nonatomic) NSString *textContent;
最后,这是我的Annotation.h文件,其中cname是应该转到&#39; myLabel&#39;
的文件#import <Foundation/Foundation.h>
#import <MapKit/MapKit.h>
@interface Annotation : NSObject <MKAnnotation>
@property(nonatomic, assign) CLLocationCoordinate2D coordinate;
@property(nonatomic, copy)NSString *title;
@property(nonatomic, copy)NSString *subtitle;
@property(nonatomic, copy)UIButton *rightButton;
@property(nonatomic, retain)NSString *cid;
@property(nonatomic, retain)NSString *cname;
@property(nonatomic, retain)NSString *ctel;
@property(nonatomic, retain)NSString *cstr;
@property(nonatomic, retain)NSString *cmon;
@property(nonatomic, retain)NSString *ctue;
@property(nonatomic, retain)NSString *cwed;
@property(nonatomic, retain)NSString *passedId;
@property(nonatomic, retain)NSString *objectId;
@end
答案 0 :(得分:0)
sender
中的[self performSegueWithIdentifier: @"openingHours" sender:ann];
参数应设置为self
。这旨在将呈现的视图控制器引用给呈现控制器。
将属性@property (nonatomic, strong) Annotation *selectedAnnotation
添加到呈现视图控制器。因此可以这样设置:
//this works fine, I get all the values
[self setSelectedAnnotation:ann];
[self performSegueWithIdentifier: @"openingHours" sender:ann];
现在您将选定的注释作为属性,可用于初始化LAOpeningHoursViewController
:
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString:@"openingHours"]) {
LAOpeningHoursViewController *secondView = (LAOpeningHoursViewController*)segue.destinationViewController;
//PFObject *tempObject = [[myMapView selectedAnnotations] objectAtIndex:0];
Annotation *ann = (id)sender;
// Setup second view here:
secondView.myLabel.text = self.selectedAnnotation.cname;
// assign further properties here...
}
}
我希望这有帮助,祝你好运!