2

可能重复:
在弹出窗口仍然可见时达到 UIPopovercontroller 解除分配

我正在创建一个通用应用程序并尝试从相机胶卷中选择一张图像。在 iPhone 上运行良好,iPad 想要弹出,所以已经这样做了,现在一直出错

-[UIPopoverController dealloc] reached while popover is still visible.

我研究过:

堆栈链接

堆栈链接

和谷歌,没有什么可以解决这个问题

现在卡住了,任何建议表示赞赏

我已经在 .h 中实现了 popover 委托

.m

 - (void)logoButtonPressed:(id)sender /////////////iPad requires seperate method ////////////////
 {                                        

if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) {

LogCmd();
UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
imagePicker.delegate = self;
imagePicker.allowsEditing = YES;
////////
imagePicker.modalPresentationStyle = UIModalPresentationCurrentContext;
////////
[self presentModalViewController:imagePicker animated:YES];


}

else

{

    // We are using an iPad
    UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];
    imagePickerController.delegate = self;
    UIPopoverController *popoverController=[[UIPopoverController alloc] initWithContentViewController:imagePickerController];
    popoverController.delegate=self;


    [popoverController presentPopoverFromRect:((UIButton *)sender).bounds inView:sender permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];


   }


  }

我现在也试过这个:

。H

 @property (strong) UIPopoverController *pop;

.m

if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) {            ///code added

LogCmd();
UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
imagePicker.delegate = self;
imagePicker.allowsEditing = YES;
[self presentModalViewController:imagePicker animated:YES];


    ///code added////////////////////////////

}

else {

    if (self.pop) {
        [self.pop dismissPopoverAnimated:YES];
    }
    // If usingiPad
    UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];

    UIPopoverController *popoverController=[[UIPopoverController alloc] initWithContentViewController:imagePickerController];


    [popoverController presentPopoverFromRect:((UIButton *)sender).bounds inView:sender permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
4

1 回答 1

8

您实例化弹出框并将其分配给此处的局部变量:

UIPopoverController *popoverController=[[UIPopoverController alloc] initWithContentViewController:imagePickerController];

一旦方法返回,变量就会超出范围并且对象被释放,因为它不再具有所有者。

你应该做的是声明一个strong属性来分配弹出框。您已经对您的pop财产这样做了。所以现在您需要做的就是在分配弹出框时,将其分配给您的属性。这使您成为对象的所有者,因此它不会被释放。

self.pop = [[UIPopoverController alloc] initWithContentViewController:imagePickerController];
[self.pop presentPopoverFromRect:((UIButton *)sender).bounds inView:sender permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];

希望这可以帮助!

于 2012-10-01T23:01:20.900 回答