Error using UIPopoverController

烈酒焚心 提交于 2019-12-23 05:27:49

问题


In my current app, I am trying to make a menu screen apear when I click a tool bar button. I found that the best way to do this is to use a UIPopoverController to show a custom view that I have created. However, something about my current code is flawed. When I run my app and push the button, I get an error that says that it is reaching dealoc while the popover is still visible. Even after looking at several examples of similar problems, I can't for the life of me figure out how to fix this. Here is my code:

-(IBAction)newMenu:(id)sender{


newTaskWindow *newTaskWin = [[newTaskWindow alloc]init];
UIView *test = [[UIView alloc]init];
test = newTaskWin.view;

UIPopoverController *taskPop = [[UIPopoverController alloc]initWithContentViewController:newTaskWin];

[taskPop setDelegate:self];
[taskPop presentPopoverFromBarButtonItem:newTaskButton permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
[taskPop setPopoverContentSize:CGSizeMake(400, 500)];

Any Ideas? Thanks in advance

}

This is the error that it produces:

 *** Terminating app due to uncaught exception 'NSGenericException', reason: '-   [UIPopoverController dealloc] reached while popover is still visible.'

*** First throw call stack:
(0x381fb8bf 0x37d471e5 0x381fb7b9 0x381fb7db 0x32065f71 0x37d430c5 0x37d44db7 0x2ecf       0x38155435 0x31cbe9eb 0x31d843cf 0x38155435 0x31cbe9eb 0x31cbe9a7 0x31cbe985 0x31cbe6f5   0x31cbf02d 0x31cbd50f 0x31cbcf01 0x31ca34ed 0x31ca2d2d 0x37f29df3 0x381cf553 0x381cf4f5   0x381ce343 0x381514dd 0x381513a5 0x37f28fcd 0x31cd1743 0x2bd5 0x2b6c)
terminate called throwing an exception(gdb)

回答1:


Try this, in the header .h file of the current class:

@property (nonatomic, retain) UIPopoverController *myPopover;

And in the implementation .m file:

//right after @implementation YourCurrentClassName
@synthesize myPopover;

And in side the -(IBAction)newMenu:(id)sender method, replace the last two lines with these:

[taskPop setPopoverContentSize:CGSizeMake(400, 500)];
self.myPopover = taskPop;
[self.myPopover presentPopoverFromBarButtonItem:newTaskButton permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
[taskPop release];  //if not using ARC



回答2:


Here Popover is a popovercontroller

if (self.popover) {
         [self.popover dismissPopoverAnimated:YES];
}



回答3:


Where have you released the taskPop object.

If it is right after the presentation, then that could be the reason.

Set the delegate and release the popoverController in the below delegate method:

- (void)popoverControllerDidDismissPopover:(UIPopoverController *)popoverController {

Again, according to Apple, this delegate method is not called if you programmatically dismiss the controller. In such cases, release the PopoverController when you dismiss it programmatically.

Hope this helps.



来源:https://stackoverflow.com/questions/8609122/error-using-uipopovercontroller

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!