如何使用MKMapView(SDK3.2)中的UIPopoverController解除在iPad上的PopoverAnimated [英] How to dismissPopoverAnimated on iPad with UIPopoverController in MKMapView (SDK3.2)
问题描述
我有一个带注释的MKMapView(也是一个UIPopoverControllerDelegate)。这个MapView在 MKTestMapView.h 文件中有一个@interface中定义的 UIPopoverController * popoverController
和一个 @property (非原子,保留)UIPopoverController * popoverController;
在 @interface
部分之外定义。该控制器在 MKTestMapView.m 文件中 @synthesized
,并在 - (void)dealloc中发布
部分。此MapView中的注释具有 rightCalloutAccessoryView
s定义如下:
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 rightCalloutAccessoryView
s 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];
...
}
现在这里有趣的部分。首先,我不确定我是否需要 maximumLabelSize
和 rect
相同的大小。我是popovercontroller的新手,所以我一直在玩这个...
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..
好吧,popover显示。现在解雇它。我可以点击mapView2上的任何地方,然后popover消失......但我需要用户点击视图中的按钮,如果他们改变任何东西。 URGH!
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!
文档显示:
要以编程方式解除弹出窗口,
调用dismissPopoverAnimated:弹出控制器的
方法。
To dismiss a popover programmatically, call the dismissPopoverAnimated: method of the popover controller.
嗯,这是问题:根据定义popoverController如何工作,你点击里面显示的弹出窗口的视图(点击按钮),但必须触发 dismissPopoverAnimated:
方法启动这个popover视图的控制器,在我的例子中, MKTestMapView.m 文件中的 popoverController
。
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.
现在,说了这么多,记住, [popoverController release]
直到:
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):
(假设我的弹出视图是一个TableView)在:
(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];
}
这是我的问题:我无法弄清楚做上述事情是否给了我一个引用
(如果有这样的话)到屏幕上的现有视图 - 因此视图是该视图的所有者popoverController。如果它很简单
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!
这应该很容易......但我迷路了。 (可能只是对我正在学习的许多iPad差异感到沮丧)。
This should be easy...yet I am lost. (probably just frustrated with so many iPad differences that I am learning).
任何人都可以解释更多吗?
Can anyone explain more?
推荐答案
我遇到了同样的问题...我在弹出窗口加载的视图顶部有一个整洁的关闭按钮(X),但它没有用。在我的通用应用程序中,它将作为新视图呈现,以便代码保留。
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.
我现在所做的是将以下内容添加到我的detailedPinView(popover加载的视图)中:
What I did now was that I added the following to my detailedPinView (the view the popover loads):
在详细的PinView.h文件中:
in the detailedPinView.h file:
@interface detailedPinView : UIViewController {
[...]
UIPopoverController *popover;
[...]
}
-(void)setPopover:(UIPopoverController*)aPopover;
在detailedPinView.m文件中:
In the detailedPinView.m file:
- (void)setPopover:(UIPopoverController*)aPopover
{
popover = aPopover;
}
X按钮使用IBAction关闭视图,这就是我在那里做的:
The X button closes the view using an IBAction, this is what I did there:
在详细的PinView.m文件中:
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 :)
这篇关于如何使用MKMapView(SDK3.2)中的UIPopoverController解除在iPad上的PopoverAnimated的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!