How to dismissPopoverAnimated on iPad with UIPopoverController in MKMapView (SDK3.2)

后端 未结 2 1694
清歌不尽
清歌不尽 2021-02-08 22:01

I have a MKMapView (also a UIPopoverControllerDelegate) with Annotations. This MapView has, in the MKTestMapView.h file, a UIPopoverController* popoverCo

相关标签:
2条回答
  • 2021-02-08 22:14

    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;
    
    0 讨论(0)
  • 2021-02-08 22:26

    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 :)

    0 讨论(0)
提交回复
热议问题