如何在 MKMapView (SDK3.2) 中使用 UIPopoverController 在 iPad 上关闭 PopoverAnimated

发布于 2024-08-29 06:01:42 字数 2608 浏览 1 评论 0原文

我有一个带注释的 MKMapView (也是一个 UIPopoverControllerDelegate)。该MapView在MKTestMapView.h文件中,在@interface中定义了一个UIPopoverController* popoverController和一个@property(非原子,保留)UIPopoverController* popoverController;@interface 部分之外定义。该控制器在 MKTestMapView.m 文件中为 @synthesized,并在 - (void)dealloc 部分中发布。此 MapView 中的注释已将 rightCalloutAccessoryView 定义为以下内容:

- (void)mapView:(MKMapView *)mapView2 annotationView:(MKAnnotationView *)aview calloutAccessoryControlTapped:(UIControl *)control{

...

CGPoint leftTopPoint = [mapView2 convertCoordinate:aview.annotation.coordinate toPointToView:mapView2];

int boxDY=leftTopPoint.y;
int boxDX=leftTopPoint.x;
NSLog(@"\nDX:%d,DY:%d\n",boxDX,boxDY);

popoverController = [[UIPopoverController alloc] initWithContentViewController:controller];
popoverController.delegate = self;
CGSize maximumLabelSize = CGSizeMake(320.0f,600.0f);

popoverController.popoverContentSize = maximumLabelSize;

CGRect rect = CGRectMake(boxDX, boxDY, 320.0f, 600.0f);

[popoverController presentPopoverFromRect:rect inView:self.view permittedArrowDirections:UIPopoverArrowDirectionRight animated:YES];


...

}

现在到了有趣的部分。首先,我不确定是否需要 maximumLabelSize 和 rect 具有相同的大小。我是弹出框控制器的新手,所以我是凭耳朵来玩的。

好吧,弹出框显示了。现在要驳回它。我可以单击 mapView2 上的任意位置,弹出窗口就会消失...但我需要用户在更改任何内容时单击视图中的按钮。呃!

文档显示:

要以编程方式关闭弹出窗口, 调用dismissPopoverAnimated: 弹出控制器的方法。

好吧,问题是:根据 popoverController 工作方式的定义,您在显示的弹出窗口的视图中单击内部(以单击按钮),但必须触发 dismissPopoverAnimated:启动此弹出窗口视图的控制器的 code> 方法,在我的例子中,是 MKTestMapView.m 文件内的 popoverController

现在,说了这么多,请记住,[popoverController release] 不会发生,直到:

- (void)dealloc {
 [popoverController release];
 [mapView release];
    [super dealloc];
}

所以,我只需在按钮内执行以下操作(凌乱但可能有效):(

假设我的弹出窗口视图是TableView)中:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
MKTestMapView * mKTestMapView = [[MKTestMapView alloc] init];
[[mKTestMapView popoverController].dismissPopoverAnimated:YES];
}

这是我的问题:我无法弄清楚执行上述操作是否为我提供了对现有视图的参考(如果有这样的事情)在屏幕上——因此视图是该 popoverController 的所有者。如果它像我一样简单,

[[[self parentView] popoverController].dismissPopoverAnimated:YES];

我会开枪自杀,因为我认为这也不是正确的语法!

这应该很容易......但我迷路了。 (可能只是对我正在学习的 iPad 的这么多差异感到沮丧)。

谁能解释更多吗?

I have a MKMapView (also a UIPopoverControllerDelegate) with Annotations. This MapView has, in the MKTestMapView.h file, a UIPopoverController* popoverController defined in the @interface and a @property (nonatomic, retain) UIPopoverController* popoverController; defined outside of the @interface section. This controller is @synthesized in the MKTestMapView.m file and it is released in the - (void)dealloc section. The Annotations in this MapView have rightCalloutAccessoryViews defined to the following:

- (void)mapView:(MKMapView *)mapView2 annotationView:(MKAnnotationView *)aview calloutAccessoryControlTapped:(UIControl *)control{

...

CGPoint leftTopPoint = [mapView2 convertCoordinate:aview.annotation.coordinate toPointToView:mapView2];

int boxDY=leftTopPoint.y;
int boxDX=leftTopPoint.x;
NSLog(@"\nDX:%d,DY:%d\n",boxDX,boxDY);

popoverController = [[UIPopoverController alloc] initWithContentViewController:controller];
popoverController.delegate = self;
CGSize maximumLabelSize = CGSizeMake(320.0f,600.0f);

popoverController.popoverContentSize = maximumLabelSize;

CGRect rect = CGRectMake(boxDX, boxDY, 320.0f, 600.0f);

[popoverController presentPopoverFromRect:rect inView:self.view permittedArrowDirections:UIPopoverArrowDirectionRight animated:YES];


...

}

Now here comes the fun part. First of all, I am not sure if I need maximumLabelSize and the rect to be the same size. I am new to the popovercontroller so I am playing this by ear..

Okay, the popover shows. Now to dismissing it. I can click anywhere on mapView2 and the popover goes away...but I need the user to click a button in the view if they change anything. URGH!

The docs show:

To dismiss a popover programmatically,
call the dismissPopoverAnimated:
method of the popover controller.

Well, here is the problem: By definition of how the popoverController works, you are clicking inside the view of the displayed popover (to click the button) but have to trigger the dismissPopoverAnimated: method of the controller that launched this popover view, in my case, the popoverController inside the MKTestMapView.m file.

Now, having said all that, remember, [popoverController release] doesn't happen until:

- (void)dealloc {
 [popoverController release];
 [mapView release];
    [super dealloc];
}

So, do i just do the following inside the button (messy but may work):

(Assuming my popover view is a TableView) In the:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
MKTestMapView * mKTestMapView = [[MKTestMapView alloc] init];
[[mKTestMapView popoverController].dismissPopoverAnimated:YES];
}

Here is my issue: I cannot figure out whether doing the above gives me a reference (if there is such a thing) to the existing view that is on the screen -- and therefore the view that is the owner of that popoverController. If it is as simple as

[[[self parentView] popoverController].dismissPopoverAnimated:YES];

I will shoot myself cos I don't think that is the correct syntax either!

This should be easy...yet I am lost. (probably just frustrated with so many iPad differences that I am learning).

Can anyone explain more?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

む无字情书 2024-09-05 06:01:42

我遇到了同样的问题...我的视图顶部有一个简洁的“关闭”按钮(X),由弹出窗口加载,但它不起作用。在我的通用应用程序中,它将显示为新视图,因此代码应该保留。

我现在所做的是将以下内容添加到我的DetailedPinView(弹出窗口加载的视图)中:

在detailedPinView.h 文件中:

@interface detailedPinView : UIViewController {
    [...]   
    UIPopoverController *popover;
    [...]
}

-(void)setPopover:(UIPopoverController*)aPopover;

在detailedPinView.m 文件中:

- (void)setPopover:(UIPopoverController*)aPopover
{
    popover = aPopover;
}

X 按钮使用IBAction 关闭视图,这就是我所做的那里:

在detailedPinView.m文件中:

-(IBAction)releaseDetailedView:(UIButton *)sender
{
    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
    {
        if (popover != nil)
        {
            [popover dismissPopoverAnimated:YES];
        }
        else {
            NSLog(@"Nothing to dismiss");
        }
    }
    else{
        [self.parentViewController dismissModalViewControllerAnimated: YES];
    }
}

在加载我的地​​图和弹出窗口视图的类中,我添加了以下代码:

[...]
-(void)mapView:(MKMapView *)theMapView annotationView:(MKAnnotationView *)pin calloutAccessoryControlTapped:(UIControl *)control
{   
    UIViewController *detailController = [[detailedPinView alloc] initWithNibName:@"detailedPinView" 
                                                                           bundle:nil 
                                                                   annotationView:pin];

    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
    {

        UIPopoverController* aPopover = [[UIPopoverController alloc] initWithContentViewController:detailController];
        [aPopover setDelegate:self];
        [aPopover setPopoverContentSize:CGSizeMake(320, 320) animated:YES];

        [detailController setPopover:aPopover];
        [detailController release];

        [mapView deselectAnnotation:pin.annotation animated:YES];

        self.popoverController = aPopover;

        [mapView setCenterCoordinate:pin.annotation.coordinate animated:YES];

        [self.popoverController presentPopoverFromRect:CGRectMake(382,498,0,0) inView:self.view  permittedArrowDirections:UIPopoverArrowDirectionUp animated:YES];
    }
    else
    {
        [self presentModalViewController: detailController animated:YES];
    }


    [detailController release];
}
[...]

我不知道这是否是您所希望的答案,我认为这可能有点凌乱的方式来做到这一点......但是给出时间安排这就像一个魅力:)

I had the same problem... I had a neat "close" button (X) in the top of my view loaded by the popover, but it didn't work. In my universal app it will be presented as a new view, so that code should stay.

What I did now was that I added the following to my detailedPinView (the view the popover loads):

in the detailedPinView.h file:

@interface detailedPinView : UIViewController {
    [...]   
    UIPopoverController *popover;
    [...]
}

-(void)setPopover:(UIPopoverController*)aPopover;

In the detailedPinView.m file:

- (void)setPopover:(UIPopoverController*)aPopover
{
    popover = aPopover;
}

The X button closes the view using an IBAction, this is what I did there:

In the detailedPinView.m file:

-(IBAction)releaseDetailedView:(UIButton *)sender
{
    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
    {
        if (popover != nil)
        {
            [popover dismissPopoverAnimated:YES];
        }
        else {
            NSLog(@"Nothing to dismiss");
        }
    }
    else{
        [self.parentViewController dismissModalViewControllerAnimated: YES];
    }
}

In the class loading the my map and the popover view I added the following code:

[...]
-(void)mapView:(MKMapView *)theMapView annotationView:(MKAnnotationView *)pin calloutAccessoryControlTapped:(UIControl *)control
{   
    UIViewController *detailController = [[detailedPinView alloc] initWithNibName:@"detailedPinView" 
                                                                           bundle:nil 
                                                                   annotationView:pin];

    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
    {

        UIPopoverController* aPopover = [[UIPopoverController alloc] initWithContentViewController:detailController];
        [aPopover setDelegate:self];
        [aPopover setPopoverContentSize:CGSizeMake(320, 320) animated:YES];

        [detailController setPopover:aPopover];
        [detailController release];

        [mapView deselectAnnotation:pin.annotation animated:YES];

        self.popoverController = aPopover;

        [mapView setCenterCoordinate:pin.annotation.coordinate animated:YES];

        [self.popoverController presentPopoverFromRect:CGRectMake(382,498,0,0) inView:self.view  permittedArrowDirections:UIPopoverArrowDirectionUp animated:YES];
    }
    else
    {
        [self presentModalViewController: detailController animated:YES];
    }


    [detailController release];
}
[...]

I don't know if the was the answer you were hoping for, I think it might be a bit of a messy way to do it... but giving the time schedule this worked like a charm :)

子栖 2024-09-05 06:01:42

这是另一个简单的解决方案。

我发现我们应该按照以下步骤来明确消除弹出窗口。

  1. 关闭弹出窗口。
  2. 释放弹出窗口加载的视图。

在 iOS8 之前,几乎所有人都可能会先释放由弹出窗口加载的视图,然后以编程方式关闭弹出窗口。
然而,在 iOS8 中,我们以相反的方式执行这些步骤。

在iOS8之前,我关闭弹出窗口的代码

// creating a popover loading an image picker
picker = [[UIImagePickerController alloc] init];
...
pickerPopover = [[UIPopoverController alloc] initWithContentViewController:picker];
[pickerPopover presentPopoverFromRect:aFrame inView:aView permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];

// dismissing the popover
[picker.view removeFromSuperview]; // (1) release a view loaded by a popover
[picker release], picker = nil;

if( UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad ) {
    [pickerPopover dismissPopoverAnimated:YES]; // (2) dismiss the popover
}

在iOS8中,关闭代码部分应更改如下,

if( UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad ) {
    [pickerPopover dismissPopoverAnimated:YES];  // (2) dismiss the popover first
}

[picker.view removeFromSuperview]; // (1) and then release the view loaded by the popover
[picker release], picker = nil;

Here is another simple solution.

I found that we should follow the following steps for clearly dismissing popovers.

  1. dismiss a popover.
  2. release a view loaded by the popover.

Before iOS8, almost all of us may release a view loaded by a popover first, and then we programmatically dismiss the popover.
However, in iOS8, we do the steps in revers.

Before iOS8, my code of dismissing a popover

// creating a popover loading an image picker
picker = [[UIImagePickerController alloc] init];
...
pickerPopover = [[UIPopoverController alloc] initWithContentViewController:picker];
[pickerPopover presentPopoverFromRect:aFrame inView:aView permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];

// dismissing the popover
[picker.view removeFromSuperview]; // (1) release a view loaded by a popover
[picker release], picker = nil;

if( UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad ) {
    [pickerPopover dismissPopoverAnimated:YES]; // (2) dismiss the popover
}

In iOS8, the dismissing code part should be changed as below,

if( UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad ) {
    [pickerPopover dismissPopoverAnimated:YES];  // (2) dismiss the popover first
}

[picker.view removeFromSuperview]; // (1) and then release the view loaded by the popover
[picker release], picker = nil;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文