Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/42.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone 带有按钮的自定义注释。DidSelectAnnotationView似乎未被调用_Iphone_Ios_Objective C_Xcode_Ipad - Fatal编程技术网

Iphone 带有按钮的自定义注释。DidSelectAnnotationView似乎未被调用

Iphone 带有按钮的自定义注释。DidSelectAnnotationView似乎未被调用,iphone,ios,objective-c,xcode,ipad,Iphone,Ios,Objective C,Xcode,Ipad,使用自定义注释视图时,调用didSelectAnnotationView时遇到问题。我希望有人能看看,帮我解决我所缺少的问题 实际上,我希望在用户触摸自定义注释屏幕截图中显示的按钮时调用一个方法,如下所示。我试图将其设置为Callout AccessoryView,但我做错了什么。请如果有人做了自定义注释视图,用户可以点击,请查看您是否可以帮助!!这让我快发疯了。我想我已经在这里发布了相关信息,如果不询问,我会编辑我的帖子 我正在添加如下自定义注释: LocationObject * obj =

使用自定义注释视图时,调用didSelectAnnotationView时遇到问题。我希望有人能看看,帮我解决我所缺少的问题

实际上,我希望在用户触摸自定义注释屏幕截图中显示的按钮时调用一个方法,如下所示。我试图将其设置为Callout AccessoryView,但我做错了什么。请如果有人做了自定义注释视图,用户可以点击,请查看您是否可以帮助!!这让我快发疯了。我想我已经在这里发布了相关信息,如果不询问,我会编辑我的帖子

我正在添加如下自定义注释:

LocationObject * obj = [m_MyObject.marLocations objectAtIndex:page];
obj.title =@"A";
[mvMapView addAnnotation:obj];
// Location Object
@interface LocationObject : NSObject <MKAnnotation>

@property (nonatomic, retain) NSString * practicename;
@property (nonatomic, retain) NSString * address1;
@property (nonatomic, retain) NSString * mapaddress1;
@property (nonatomic, retain) NSString * address2;
@property (nonatomic, retain) NSString * city;
@property (nonatomic, retain) NSString * state;
@property (nonatomic, retain) NSString * zip;
@property (nonatomic, retain) NSString * phone;
@property (nonatomic, retain) NSString * fax;
@property (nonatomic, retain) NSString * email;
@property (nonatomic, retain) NSString * longitude;
@property (nonatomic, retain) NSString * latitude;
@property (nonatomic, assign) CLLocationCoordinate2D coordinate;
@property (nonatomic, copy) NSString *title;
@property (nonatomic, copy) NSString *subtitle;

@end // End Location Object
- (MKAnnotationView *)mapView:(MKMapView *)map viewForAnnotation:(id <MKAnnotation>)annotation
{
    if ([annotation isKindOfClass:[MKUserLocation class]])
    {
        [map.userLocation setTitle:@"I am here"];
        return nil;
    }

    static NSString* AnnotationIdentifier = @"annotationViewID";
    ViewDirectionsAnnotationView * annotationView = [[ViewDirectionsAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:AnnotationIdentifier];
    int page = floor((svOfficesScrollView.contentOffset.x - svOfficesScrollView.frame.size.width / 2) / svOfficesScrollView.frame.size.width) + 1;
    LocationObject * obj = [m_DentistObject.marLocations objectAtIndex:page];
    double dLatitude = [obj.latitude doubleValue];
    double dLongitude = [obj.longitude doubleValue];
    CLLocationCoordinate2D coordinate = CLLocationCoordinate2DMake(dLatitude, dLongitude);
    ((LocationObject*)annotation).coordinate = coordinate;
    annotationView.lbPracticeName.text = obj.practicename;
    annotationView.lbStreet.text = obj.address1;
    NSString * sCityStateZip = [NSString stringWithFormat:@"%@, %@ %@", obj.city, obj.state, obj.zip];
    annotationView.lbCityStateZip.text = sCityStateZip;
    annotationView.lbPhoneNumber.text = obj.phone;
    annotationView.canShowCallout = YES;
    annotationView.annotation = annotation;
    annotationView.rightCalloutAccessoryView = (UIButton *)[annotationView viewWithTag:1]; //[UIButton buttonWithType:UIButtonTypeDetailDisclosure];

    return annotationView;
}
@interface ViewDirectionsAnnotationView : MKAnnotationView
{
    CLLocationCoordinate2D    coordinate;
    NSString        * title;
    NSString        * subtitle;
}

@property (nonatomic, retain) IBOutlet UIView * loadedView;
@property (weak, nonatomic) IBOutlet UILabel  * lbPracticeName;
@property (weak, nonatomic) IBOutlet UILabel  * lbStreet;
@property (weak, nonatomic) IBOutlet UILabel  * lbCityStateZip;
@property (weak, nonatomic) IBOutlet UILabel  * lbPhoneNumber;

@end
上面使用的我的位置对象定义如下:

LocationObject * obj = [m_MyObject.marLocations objectAtIndex:page];
obj.title =@"A";
[mvMapView addAnnotation:obj];
// Location Object
@interface LocationObject : NSObject <MKAnnotation>

@property (nonatomic, retain) NSString * practicename;
@property (nonatomic, retain) NSString * address1;
@property (nonatomic, retain) NSString * mapaddress1;
@property (nonatomic, retain) NSString * address2;
@property (nonatomic, retain) NSString * city;
@property (nonatomic, retain) NSString * state;
@property (nonatomic, retain) NSString * zip;
@property (nonatomic, retain) NSString * phone;
@property (nonatomic, retain) NSString * fax;
@property (nonatomic, retain) NSString * email;
@property (nonatomic, retain) NSString * longitude;
@property (nonatomic, retain) NSString * latitude;
@property (nonatomic, assign) CLLocationCoordinate2D coordinate;
@property (nonatomic, copy) NSString *title;
@property (nonatomic, copy) NSString *subtitle;

@end // End Location Object
- (MKAnnotationView *)mapView:(MKMapView *)map viewForAnnotation:(id <MKAnnotation>)annotation
{
    if ([annotation isKindOfClass:[MKUserLocation class]])
    {
        [map.userLocation setTitle:@"I am here"];
        return nil;
    }

    static NSString* AnnotationIdentifier = @"annotationViewID";
    ViewDirectionsAnnotationView * annotationView = [[ViewDirectionsAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:AnnotationIdentifier];
    int page = floor((svOfficesScrollView.contentOffset.x - svOfficesScrollView.frame.size.width / 2) / svOfficesScrollView.frame.size.width) + 1;
    LocationObject * obj = [m_DentistObject.marLocations objectAtIndex:page];
    double dLatitude = [obj.latitude doubleValue];
    double dLongitude = [obj.longitude doubleValue];
    CLLocationCoordinate2D coordinate = CLLocationCoordinate2DMake(dLatitude, dLongitude);
    ((LocationObject*)annotation).coordinate = coordinate;
    annotationView.lbPracticeName.text = obj.practicename;
    annotationView.lbStreet.text = obj.address1;
    NSString * sCityStateZip = [NSString stringWithFormat:@"%@, %@ %@", obj.city, obj.state, obj.zip];
    annotationView.lbCityStateZip.text = sCityStateZip;
    annotationView.lbPhoneNumber.text = obj.phone;
    annotationView.canShowCallout = YES;
    annotationView.annotation = annotation;
    annotationView.rightCalloutAccessoryView = (UIButton *)[annotationView viewWithTag:1]; //[UIButton buttonWithType:UIButtonTypeDetailDisclosure];

    return annotationView;
}
@interface ViewDirectionsAnnotationView : MKAnnotationView
{
    CLLocationCoordinate2D    coordinate;
    NSString        * title;
    NSString        * subtitle;
}

@property (nonatomic, retain) IBOutlet UIView * loadedView;
@property (weak, nonatomic) IBOutlet UILabel  * lbPracticeName;
@property (weak, nonatomic) IBOutlet UILabel  * lbStreet;
@property (weak, nonatomic) IBOutlet UILabel  * lbCityStateZip;
@property (weak, nonatomic) IBOutlet UILabel  * lbPhoneNumber;

@end
上述视图的xib如下所示:

但是,我的didSelectAnnotationView从未被调用。有人能解释我遗漏了什么吗

求求你,谢谢你的帮助,这让我快发疯了。我可能完全错了,但是自定义视图确实出现了,所以我想我很接近了

谢谢你的帮助

更新:

我忘了提到代表。我已经检查了代表多次,相信我是对的。在mapView所在的ViewController.h文件中,我使用了,并且在同一个.m文件中设置了[mapView setDelegate:self],我在该文件中实现了viewForAnnotation方法并选择了AnnotationView。调用viewForAnnotation,但不调用另一个

另一个有趣的信息是,如果我单击(触摸)自定义注释下方,则会调用didSelectAnnotationView。我想现在我更糊涂了

更新#2:

我的xib是这样加载的:

在注释视图中,我将initWithAnnotation称为initWithAnnotation。该方法如下:

- (id)initWithAnnotation:(id <MKAnnotation>)annotation reuseIdentifier:(NSString*)reuseIdentifier
{
    self = [super initWithAnnotation:annotation reuseIdentifier:reuseIdentifier];
    if (self != nil)
    {
        [[NSBundle mainBundle] loadNibNamed:@"DirectionsAnnotation" owner:self options:nil];
        if (loadedView)
        {
            [loadedView setFrame:CGRectMake(-(loadedView.frame.size.width/2), -(loadedView.frame.size.height), loadedView.frame.size.width, loadedView.frame.size.height)];
            [self addSubview:loadedView];
        }
    }
    return self;
}
-(id)initWithAnnotation:(id)注释重用标识符:(NSString*)重用标识符
{
self=[super initWithAnnotation:annotation-reuseIdentifier:reuseIdentifier];
if(self!=nil)
{
[[NSBundle mainBundle]loadNibNamed:@“DirectionsAnnotation”所有者:自选项:nil];
如果(loadedView)
{
[loadedView setFrame:CGRectMake(-(loadedView.frame.size.width/2),-(loadedView.frame.size.height),loadedView.frame.size.width,loadedView.frame.size.height)];
[自添加子视图:loadedView];
}
}
回归自我;
}

需要将显示地图视图的视图设置为代理

您的didSelectAnnotationView在哪里?确保它实现了委托协议。例如

@interface MyView : UIViewController <MKMapViewDelegate>
...
@end

我创建地图视图的视图是一个ViewController,并设置为代理,并且我的头文件中有该视图的。我已经检查过一百次了,我想一定是别的原因。还有其他建议吗?如何准确加载xib?您是否正在设置注释视图的框架?关于viewForAnnotation中的代码,有一点是不相关的:大部分代码效率低下,不必要地复杂。我不使用annotation views框架做任何事情。你能解释一下什么是复杂的吗?如果你是指我的代码,我愿意接受任何建议。我已经更新了我的帖子以显示如何加载xib。请尝试将注释视图的框架设置为xib的大小。xib可能大于默认框架,并且不处理外部接触。在viewForAnnotation中,不必要的复杂性在于通过计算页面等来“查找”注释。注释已经作为方法的参数提供——您不需要查找它或设置其坐标。抱歉,当我更新帖子时,我注意到我在那里设置了框架。你可以看到代码。。。看起来对吗?啊,是的,我注意到了,并且已经纠正了!!!谢谢你指出这一点,这让我感觉很好,我做了正确的事情!!也许笔尖中的一个视图正在捕捉触摸?我的最佳猜测是
loadedView
,请尝试取消选中nib中的“已启用用户交互”。