Objective-C的新手,但我试图在xCode中为我的应用添加音乐选择器,你点击它的东西,它打开了用户音乐库,让他们选择一首歌,但我可以&找出如何做到这一点,我所见过的所有文档似乎都不起作用。例如,
MPMediaPickerController *picker =
[[MPMediaPickerController alloc]
initWithMediaTypes: MPMediaTypeAnyAudio]; // 1
[picker setDelegate: self]; // 2
[picker setAllowsPickingMultipleItems: YES]; // 3
picker.prompt =
NSLocalizedString (@"Add songs to play",
"Prompt in media item picker");
[myController presentModalViewController: picker animated: YES]; // 4
[picker release];
直接来自Apple的帮助文档。当我在我的应用程序中输入此内容时,我会收到错误:
mediaPicker.delegate = self; // Error: Assigning to 'id<MPMediaPickerControllerDelegate?' from incompatible type "ViewController *const__stron"
和
[self presentModalViewController:mediaPicker animated:YES]; // Error: 'presentModalViewController:animated:' is deprecated: first deprecated in iOS 6.0
我觉得我需要对mediaPicker.delegate = self;
行做些什么,但不知道是什么。有什么帮助吗?
答案 0 :(得分:1)
更改此行:
mediaPicker.delegate = self; // Error: Assigning to 'id<MPMediaPickerControllerDelegate?' from incompatible type "ViewController *const__stron"
到
mediaPicker.delegate = id<MPMediaPickerControllerDelegate>self;
或者您可以转到自我类的Header文件,并通过将其编写为MPMediaPickerControllerDelegate
-
@interface CLassName : UIViewController <MPMediaPickerControllerDelegate> {
//code
}
关于你的第二个问题,请使用此方法:
- (void)presentViewController:(UIViewController *)viewControllerToPresent animated: (BOOL)flag completion:(void (^)(void))completion NS_AVAILABLE_IOS(5_0)
您使用的方法已被弃用,因此最好使用这个新方法。使用它:
[self presentViewController:mediaPicker animated:YES completion:nil];
如果需要,您可以将动画更改为NO
,就像完成块一样。如果您需要在动画完成后执行某些操作,则可以在此块中编写代码。
希望这会有所帮助。欢呼声。
<强> ***** EDIT ***** 强>
假设您的班级名称为TestMediaPickerController
。然后转到TestMediaPickerController.h
文件,然后在已声明接口的头文件中,执行以下操作:
首先将其添加到标题中。
#import <MediaPlayer/MediaPlayer.h>
然后
@interface TestMediaPickerController : UIViewController <MPMediaPickerControllerDelegate> { //Adding this `<MPMediaPickerControllerDelegate>` here makes your TestMediaPickerController Class confirm to the Protocol MPMediaPickerControllerDelegate.
//code
}
您遇到此问题似乎无法设置MPMediaPickerControllerDelegate。