Search code examples
iosobjective-cuiviewcontrolleruipopovercontroller

calling dismissPopoverAnimated in viewWillDisappear crashes application


I have two ViewControllers. Let's call them no1 and no2. In no2 I have PopoverViewController with some options and instance of NSTimer. Timer function is calling popToViewController 3 seconds after popup is presented if nothing is clicked in the popover, which is returning user to no1 ViewController. Problem is when this function is triggered, screen is changed to the no1, but application crashes without error message below.

PopoverViewController doesn't have delegate and it is registered as property of second VC as:

@property (nonatomic)UIPopoverController *optionsPopover;

Does anyone have any idea why there is no crash report available? And if there is no reference to the popover why it is crashing?

Implementation in viewWillDisappear looks like this:

if([_optionsPopover isPopoverVisible]){
    [_optionsPopover dismissPopoverAnimated:NO];
    _optionsPopover = nil;
}

I tried forcing UI to update on main thread (below code), but the result is the same. Crash still exists.

dispatch_async(dispatch_get_main_queue(), ^{
    if([_optionsPopover isPopoverVisible]){
        [_optionsPopover dismissPopoverAnimated:NO];
        _optionsPopover = nil;
    }
});

Solution

  • Please try to make strong reference of UIPopoverController

    @property (nonatomic,retain)UIPopoverController *optionsPopover;
    

    call below method in - (void)viewDidDisappear:(BOOL)animated instead of viewwilldisappear -

    - (void)viewDidDisappear:(BOOL)animated {
    if([_optionsPopover isPopoverVisible]){
        [_optionsPopover dismissPopoverAnimated:NO];
        _optionsPopover = nil;
    }
    }