UIPopover on ARC

2019-08-05 03:55发布

问题:

I am using ARC on an iPad app with the code below, the popover flashes on the screen, but doesn't stay. What I am doing wrong? Please help

- (IBAction)photoLibraryAction:(id)sender

{   

   UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
   [imagePicker setDelegate:self];

   UIPopoverController *pop1 = [[UIPopoverController alloc]     initWithContentViewController:imagePicker];
    [pop1 setDelegate:self];
    [pop1 presentPopoverFromBarButtonItem:sender  permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
    [pop1 setPopoverContentSize:CGSizeMake(320, 400)];
}


    if  ([pop1 isPopoverVisible])
    {
        // Popover is not visible
        [pop1 dismissPopoverAnimated:YES];

    }

}

回答1:

In ARC, pop1 will be released right after -photoLibraryAction: returns, because ARC doesn't know that -presentPopoverFromBarButtonItem:permittedArrowDirections: makes the object usable beyond its scope.
You'll have to add an instance variable for your popover controller so ARC doesn't release it. Your if-statement is invalid, too, because when the method returns, pop1 is no longer available for you to use. You'll have to use an instance variable there as well.