UIPopovercontroller dealloc reached while popover is still visible

前端 未结 3 1707
误落风尘
误落风尘 2020-12-04 09:35

I assure you that I did look for an answer in SO for my question but none of them were helpful. Here I got a simple code that should present a UIImagePickerController<

相关标签:
3条回答
  • 2020-12-04 09:56

    UIPopoverControllers should always be held in an instance variable. It is a good practice to create a strong property for it.

    UPDATE:

    As of iOS 8 you should be using UIPopoverPresentationController. Then you don't need to keep a reference to the popover because it is managed by the presentation controller.

    Code example (works both on iPhone and iPad):

    UIImagePickerController *picker = [[UIImagePickerController alloc] init];
    picker.delegate = self;
    picker.sourceType = UIImagePickerControllerSourceTypeCamera;
    picker.allowsEditing = YES;
    picker.modalPresentationStyle = UIModalPresentationPopover;
    UIPopoverPresentationController* popoverPC = picker.popoverPresentationController;
    popoverPC.barButtonItem = bbItem;
    popoverPC.permittedArrowDirections = UIPopoverArrowDirectionAny;
    [self presentViewController:picker animated:YES completion:nil];
    
    0 讨论(0)
  • 2020-12-04 10:00

    When the function exits there are no other reference to the popover controller, so it's deallocated too early.

    Try adding it as a member of your class instead.

    Tim

    0 讨论(0)
  • 2020-12-04 10:15

    Adding what @phix23 answered, create *poc property like this:

    @property (nonatomic, retain) IBOutlet UIPopoverController *poc;
    

    and then change

    UIPopoverController *poc = [[UIPopoverController alloc] 
                                initWithContentViewController:picker];
    

    for

    self.poc = [[UIPopoverController alloc] 
                                initWithContentViewController:picker];
    
    0 讨论(0)
提交回复
热议问题