ios UIPopovercontroller dealloc 在 popover 仍然可见时达到

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8895071/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 16:22:48  来源:igfitidea点击:

UIPopovercontroller dealloc reached while popover is still visible

iosmemory-managementuipopovercontrollerautomatic-ref-counting

提问by Mikayil Abdullayev

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 UIImagePickerControllerwithin a UIPopoverController:

我向您保证,我确实在 SO 中为我的问题寻找了答案,但没有一个有帮助。在这里,我得到了一个简单的代码,它应该在 aUIImagePickerController中显示一个UIPopoverController

-(void)takePicture:(id)sender{
UIImagePickerController *picker=[[UIImagePickerController alloc] init];
picker.delegate=self;
picker.sourceType=UIImagePickerControllerSourceTypeCamera;
picker.allowsEditing=YES;
UIPopoverController *poc=[[UIPopoverController alloc] 
                            initWithContentViewController:picker];
[poc presentPopoverFromBarButtonItem:bbItem 
            permittedArrowDirections:UIPopoverArrowDirectionAny
                            animated:NO];
}

Now, even from the first time I get [UIPopoveController dealloc]reached while... error and the program crashes. I'm not doing any retain,relase or autoreleases as per ARC. Is there any special consideration with UIPopoverControllerswhen benefitting from ARC?

现在,即使从我第一次[UIPopoveController dealloc]到达时...错误并且程序崩溃。我没有按照 ARC 进行任何保留、发布或自动发布。UIPopoverControllers从 ARC 中受益时有什么特别的考虑吗?

回答by Felix

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

UIPopoverControllers 应该始终保存在一个实例变量中。为它创建一个强大的属性是一个很好的做法。

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.

从 iOS 8 开始,您应该使用UIPopoverPresentationController. 然后你不需要保留对 popover 的引用,因为它是由演示控制器管理的。

Code example (works both on iPhone and iPad):

代码示例(适用于 iPhone 和 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];

回答by tarmes

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

当函数退出时,没有其他对 popover 控制器的引用,所以它过早地被释放。

Try adding it as a member of your class instead.

尝试将其添加为班级成员。

Tim

蒂姆

回答by orafaelreis

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

添加@phix23 回答的内容,创建 *poc 属性,如下所示:

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

and then change

然后改变

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

for

为了

self.poc = [[UIPopoverController alloc] 
                            initWithContentViewController:picker];