我在尝试使用 presentModalViewController 呈现 MPMediaPickerController 时遇到了一个奇怪的问题。我让它工作得很好,但最近(也许从 3.1 开始,我不完全记得它上次工作的时间)MPMediaPickerController 只是拒绝显示自己。屏幕保持黑色,顶部只有状态栏,我被迫退出应用程序。
这是我的代码:
// Show the music picker, allowing the user to create a playlist
- (void) showMusicPicker;
{
[[Director sharedDirector] pause];
[[Director sharedDirector] detach];
musicView = [[[UIView alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
[window addSubview:musicView];
musicController = [[UIViewController alloc] init];
[musicController setView:musicView];
[musicController setModalTransitionStyle: UIModalTransitionStyleCoverVertical];
MPMediaPickerController *picker =
[[MPMediaPickerController alloc] initWithMediaTypes: MPMediaTypeMusic];
picker.delegate = self;
picker.allowsPickingMultipleItems = YES;
picker.prompt = @"Select songs to play";
// The media item picker uses the default UI style, so it needs a default-style
// status bar to match it visually
[[UIApplication sharedApplication] setStatusBarHidden:NO animated: YES];
[musicController presentModalViewController: picker animated: YES];
[picker release];
}
如您所见,它大部分是从 Apple 的示例中逐字复制的,并进行了一些修改以使其与 Cocos2D 一起使用。代码中的window变量为应用程序窗口,在init方法中保留:
// Remember the window that the director is attached in
window = [[[[Director sharedDirector] openGLView] window] retain];
该方法的其余部分似乎工作正常......导演从窗口分离并出现状态栏。但是 MPMediaPickerController 应该出现在屏幕上,但它没有。正如我所提到的,不久前它工作得很好,所以我不知道这里发生了什么。
感谢您的任何帮助,您可以提供。
编辑:我注意到如果我注释掉这些行:
[[Director sharedDirector] pause];
[[Director sharedDirector] detach];
[window addSubview:musicView];
并将它们替换为:
[[[Director sharedDirector] openGLView] addSubview: musicView]
...然后选择器控制器显示为应有的样子。这是我在 3.0 发布后第一次实现 iPod 库访问时添加视图的方式,因为它比当前方式更简单。然而,这种方法的问题在于,一旦选择器控制器被解除,所有在触摸事件上报告给我的触摸坐标都变得不准确——它们比它们应该的高出大约 20 个像素(纵向方向)。我是不确定这是 Cocos2D 的错误还是什么,但它几乎排除了直接在 Director 的 openGLView 上显示控制器的可能性。