iPad popovers and Cocos2d?

Forums Programming iPhone SDK / UIKit / Objective-C / Xcode iPad popovers and Cocos2d?

This topic contains 4 replies, has 3 voices, and was last updated by  whitecastleroad 3 years, 5 months ago.

Viewing 5 posts - 1 through 5 (of 5 total)
Author Posts
Author Posts
May 1, 2010 at 4:52 am #221460


Has anyone used the default popup mechanism with cocos2d? I am writing a board game (my first) that doesn’t necessarily need my own custom popups–figured that any extra HIG features I have could only help.

After a couple (dozen?) failed attempts, I’ve been able to generate a popup on helloWorld thanks to the example at http://blog.blackwhale.at/2010/04/show-a-custom-popover-view/

I’ve been able to use the following as a CCMenu callback:

-(void) actionPerformed:(id)sender {

NSLog(@”called actionPerformed”);

//build our custom popover view

UIViewController* popoverContent = [[UIViewController alloc]


UIView* popoverView = [[UIView alloc]

initWithFrame:CGRectMake(0, 0, 200, 300)];

popoverView.backgroundColor = [UIColor greenColor];

popoverContent.view = popoverView;

//resize the popover view shown

//in the current view to the view’s size

popoverContent.contentSizeForViewInPopover =

CGSizeMake(200, 300);

//create a popover controller

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

//present the popover view non-modal with a

//refrence to the button pressed within the current view

[popoverController presentPopoverFromRect:CGRectMake(200, 200, 200, 200)

inView:[[CCDirector sharedDirector]openGLView]



//release the popover content

[popoverView release];

[popoverContent release];


But I have absolutely no luck trying to run a CCLayer inside the UIView while the main window is still the current scene. If all else fails, I may just take the easy way out and build things in interface builder. But my OCD still wants me to get this working.

Can anyone help?

May 3, 2010 at 5:11 pm #281751



Is anyone even using any HIG recommendations, or are we just taking the “we’re games–we’re exempt” stand?

May 3, 2010 at 5:58 pm #281752



here is some code I use to use a choose a photo from the library on the iPad and the iPhone. Perhaps it can help you since I use a popover.

Please note that there is no direct reference to UIPopoverController (I use id) because I am creating an universal app. So the code is checking if it is an ipad or not.

This code is running inside a CCLayer

NOTE THAT IT IS “ALPHA” CODE (from thos afternoon). I’ve not tested it extensively!

id *_popover;
UIImagePickerController *_picker;


[[CCDirector sharedDirector] pause];
[[CCDirector sharedDirector] stopAnimation];

//for IPAD
if ([ [ [UIDevice currentDevice] model] rangeOfString:@"iPad"].location != NSNotFound){

_picker = [[UIImagePickerController alloc]init];
_picker.delegate = self;
_picker.sourceType = sourceType;
_picker.wantsFullScreenLayout = YES;

//to work with 3.1 we cannot create the class directly
_popover = [[NSClassFromString(@"UIPopoverController") alloc] initWithContentViewController:_picker];
[_popover setDelegate:self];

CGSize winsize = [[CCDirector sharedDirector] winSize];
[_popover setPopoverContentSize:CGSizeMake(winsize.width,winsize.height) animated:NO];

[_popover presentPopoverFromRect:CGRectMake(0,0,10,10) inView:[[CCDirector sharedDirector]openGLView]
permittedArrowDirections:UIPopoverArrowDirectionUp animated:YES];

_picker = [[UIImagePickerController alloc]init];
_picker.delegate = self;
_picker.sourceType = sourceType;
_picker.wantsFullScreenLayout = YES;
[_picker presentModalViewController:_picker animated:YES];

[[[CCDirector sharedDirector] openGLView] addSubview:_picker.view];


-(void)imagePickerController:(UIImagePickerController *)picker
didFinishPickingMediaWithInfo:(NSDictionary *)info{

CGSize winSize = [[CCDirector sharedDirector] winSize];
UIImage *newImage = [info objectForKey:@"UIImagePickerControllerOriginalImage"];
CGSize imgsize=[newImage size];

//resize it if it is larger than the screen
if (imgsize.width>winSize.width){

newImage=[self imageWithImage:newImage scaledToSize:CGSizeMake(winSize.width, imgsize.height*(winSize.width/imgsize.width)) ];
if (imgsize.height>winSize.height){

newImage=[self imageWithImage:newImage scaledToSize:CGSizeMake(imgsize.width*(winSize.height/imgsize.height),
winSize.height) ];

// Dismiss UIImagePickerController and release it
[picker dismissModalViewControllerAnimated:YES];
[picker.view removeFromSuperview];
[picker release];
//remove image if already exist
[self removeImage];
// Let's create a sprite now that we have an image
_imageFromPicker = [CCSprite spriteWithCGImage:newImage.CGImage];
_imageFromPicker.position= ccp(winSize.width/2,winSize.height/2);
[self addChild:_imageFromPicker z:0];

if ([ [ [UIDevice currentDevice]
rangeOfString:@"iPad"].location != NSNotFound){

[_popover dismissPopoverAnimated:YES];
[_popover release];

[[CCDirector sharedDirector] startAnimation];
[[CCDirector sharedDirector] resume];

- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
// Dismiss the image selection
[picker dismissModalViewControllerAnimated:YES];
[picker.view removeFromSuperview];
[picker release];

[[CCDirector sharedDirector] startAnimation];
[[CCDirector sharedDirector] resume];

//for Ipad UIPopoverController if there is a cancel when the user click outside the popover
- (void)popoverControllerDidDismissPopover:(UIPopoverController *)popoverController{

[_picker dismissModalViewControllerAnimated:YES];
[_picker.view removeFromSuperview];
[_picker release];

[[CCDirector sharedDirector] startAnimation];
[[CCDirector sharedDirector] resume];

May 6, 2010 at 7:44 pm #281753



I didn’t want to be rude and not thank you. I have been out of commission this week and am only starting to look at your method now.

Will post my results.



November 5, 2010 at 11:48 am #281754


I find that popoverControllerDidDismissPopover is never called. I assume that inView:[[CCDirector sharedDirector]openGLView] is the cause. When the UIPopupViewController is dismissed automatically nothing gets dealloc’d. Anyone figure out how to make this work?

Viewing 5 posts - 1 through 5 (of 5 total)

You must be logged in to reply to this topic.