10

我有一个地图视图,我annotations根据popovercontroller. 当我在外部触摸时popover,它会正确关闭并调用该delegate方法popoverControllerDidDismissPopover: 我遇到的问题是,当我在popover(在弹出窗口视图中触摸)切换开关时,如果我annotations从地图中删除它,它的行为正确并且弹出窗口保持可见,但如果我将其添加annotations到地图视图中,则会popover消失并且delegate不会调用该方法。有没有人遇到过这种行为?

开关的 on 和 off 代码之间的唯一区别是一个annotations从数组中删除 ,而另一个添加annotations. 这只是添加annotations到地图视图时的问题。任何帮助或建议将不胜感激。

这是popover显示的方式:

-(IBAction)toggleMapFiltersView:(id)sender
 {
    LayerPopoverViewController *popOverViewController = [[LayerPopoverViewController alloc] init];
    [popOverViewController setDelegate:self];
    [popOverViewController setBranchesShowing:branchesShowing];
    [popOverViewController setSchoolsShowing:schoolsShowing];

    [layersButton setSelected:YES];

    popoverController = [[UIPopoverController alloc]   initWithContentViewController:popOverViewController];


    [popoverController setDelegate:self];
    [popOverViewController release];
    [popoverController presentPopoverFromRect:layersButton.frame
                                       inView:[self view] 
                     permittedArrowDirections:UIPopoverArrowDirectionAny 
                                     animated:YES];
}

这是从弹出视图中调用的方法:

-(IBAction)toggleSchools:(id)sender
{
    if ([self.delegate respondsToSelector:@selector(didChangeSchoolsDisplaySettingWithVisible:)])
    {
        if ([schoolsSwitch isOn])
        {
        [self.delegate didChangeSchoolsDisplaySettingWithVisible:YES];
             self.schoolsShowing = YES;
        }
        else
        {
            [self.delegate didChangeSchoolsDisplaySettingWithVisible:NO];
            self.schoolsShowing = NO;
        }
    }
}

这就是它所指的方法:

-(void)didChangeSchoolsDisplaySettingWithVisible:(BOOL)visible
{
    if (visible == YES)
    {
        schoolsShowing = YES;
        if (self.schoolArray != nil && [self.schoolArray count] > 0)
        {
            for (MySchool *school in self.schoolArray)
            {
                [mapView addAnnotation:school];
            }
        }
    }
    else
    {
        schoolsShowing = NO;
        for (id<MKAnnotation> annotation in mapView.annotations)
        {
            if ([annotation isKindOfClass:[MySchool class]])
            {
                [mapView removeAnnotation:annotation];
            }
        }
    }
}
4

2 回答 2

0

为什么首先使用弹出框?您可以更轻松地控制 customView。

您可以创建一个自定义 popupView,并将其放置在您喜欢的坐标上方。弹出窗口有一个关闭按钮,并委托调用此操作。

这是一些代码(在此示例中,坐标来自标记,mapView 是谷歌的)。您还必须将坐标转换为 CGPoint。

UIAnnotationView *annotation = [[UIAnnotationView alloc] initWithFrame:CGRectMake(0,0,100,100)];
                  annotation.delegate = self;
                  annotation.tag = 101;

CGPoint point = [mapView.projection pointForCoordinate:marker.position];
        annotation.origin =  CGPointMake(point.x - annotation.width / 2, point.y - annotation.height - MARKER_DEFAULT_SIZE);
        [mapView addSubview:annotation];

UIAnotaionView closeButtonClicked 委托:

- (void)annotationViewCloseButtonClicked:(UIAnnotationView *)annotationView
{
    [[_mapView viewWithTag:101] removeFromSuperview];
}

要在触摸地图中的其他点时关闭 popupView,请覆盖此委托:

- (void)mapView:(GMSMapView *)mapView didTapAtCoordinate:(CLLocationCoordinate2D)coordinate
{
    [[_mapView viewWithTag:101] removeFromSuperview];
}
于 2013-07-16T15:07:51.740 回答
0

您是否尝试过另一种委托方法

- (BOOL)popoverControllerShouldDismissPopover:(UIPopoverController *)popoverController {
    // do some stuff here and decide whether to dismiss or not
    return YES; //or No depending on your condition
}

它会询问您是否要关闭 popOver 控制器。每次 popoverviewcontroller 即将关闭时都会调用它

否则,您可以在向 mapview 添加注释时调用方法。无论如何,您知道弹出窗口正在解雇。当弹出控制器关闭时执行相同的工作。正如你正在做的那样

- (void)popoverControllerDidDismissPopover:(UIPopoverController *)popoverController

我希望这可以帮助你

于 2013-11-28T13:50:49.267 回答