iphone - UIPopoverController 在没有委托(delegate)调用的情况下解散

标签 iphone ios mkmapview uipopovercontroller

我有一个 map View ,我根据 popovercontroller 中的开关添加和删除 annotations。当我触摸 popover 外部时,它会正确关闭并调用 delegate 方法 popoverControllerDidDismissPopover: 我遇到的问题是,当我切换在 popover 中切换(在 popover View 中触摸),如果我从 map 中删除 annotations 它的行为正确并且 popover 保持可见但如果我添加 annotations 到 map View ,然后 popover 消失并且 delegate 方法不被调用。以前有人遇到过这种行为吗?

开关的 on 和 off 代码之间的唯一区别是,一个从数组中删除 annotations,而另一个添加 annotations。这只是在将 annotations 添加到 map View 时出现的问题。如有任何帮助或建议,我们将不胜感激。

这是 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];
}

这是从弹出 View 调用的方法:

-(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];
            }
        }
    }
}

最佳答案

为什么首先使用弹出窗口?您可以更轻松地控制自定义 View 。

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

这是一些代码(在这个例子中,坐标来自一个标记,而 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];

UIAnnotaionView closeButtonClicked 委托(delegate):

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

要在触摸 map 中的其他点时关闭弹出 View ,请覆盖此委托(delegate):

- (void)mapView:(GMSMapView *)mapView didTapAtCoordinate:(CLLocationCoordinate2D)coordinate
{
    [[_mapView viewWithTag:101] removeFromSuperview];
}

关于iphone - UIPopoverController 在没有委托(delegate)调用的情况下解散,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10341579/

相关文章:

objective-c - 在 Objective-C 中比较两次的最好/最简单的方法是什么?

ios - 如何从框架的原始中心放大/缩小绘制的圆圈?

ios - tableview RowInSection fatal error swift

c# - 如何从 Xamarin.Forms 应用程序修改平台特定属性?

ios - MKMapView 放置在 UITableViewCell 内时不显示位置

ios - 两点坐标之间的角度以相应地旋转图像

swift - 从 setUserTrackingMode(.Follow, animated : true)

iphone - 是否可以使用 XCode 在 iPhone 上调试 iPhone 应用程序的临时版本?

ios - 如何在iOS中设置垂直页面指示器

iphone - 在 iPhone 中的电子邮件编辑器屏幕中隐藏字段