NSInvalidArgumentException“发送到实例的无法识别的选择器”(使用 MPMoviePlayerController)

Posted

技术标签:

【中文标题】NSInvalidArgumentException“发送到实例的无法识别的选择器”(使用 MPMoviePlayerController)【英文标题】:NSInvalidArgumentException "Unrecognized selector sent to instance" (using MPMoviePlayerController) 【发布时间】:2011-11-16 11:59:08 【问题描述】:

嗯,我在一个 RootViewController 中有一个 TableView 和一个 DetailViewController 用于显示单条记录的信息。 根据本指南,在详细信息页面中,我必须播放多媒体文件,并且我正在使用框架 MediaPlayer: http://www.techotopia.com/index.php/Video_Playback_from_within_an_ios_4_iPhone_Application

似乎一切正常,但是当我点击播放按钮时出现此错误:

 -[DetailsViewController playmovie]: unrecognized selector sent to instance 0x9117f60

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[DetailsViewController playmovie]: unrecognized selector sent to instance 0x9117f60'

这些是我的文件:

在 AppDelegate 中我使用这个导航控制器:

[...]

// Create a table view controller
    RootViewController *rootViewController = [[RootViewController alloc]
                                              initWithStyle:UITableViewStylePlain];

    rootViewController.managedObjectContext = context;
    rootViewController.entityName = @"Porti";


    UINavigationController *aNavigationController = [[UINavigationController alloc]
                                                     initWithRootViewController:rootViewController];

    self.navigationController = aNavigationController;


    UIBarButtonItem *homeButton;
    homeButton = [[[UIBarButtonItem alloc] initWithTitle:@"              Inizio              " style:UIBarButtonItemStyleBordered target:self action:@selector(home)] autorelease];

    UIBarButtonItem *barButton;
    barButton = [[[UIBarButtonItem alloc] initWithTitle:@"      Mappa dei porti       " style:UIBarButtonItemStyleBordered target:self action:@selector(caricamappa)] autorelease];

    [toolbar setItems:[NSArray arrayWithObjects: homeButton, barButton, nil]];


    [window addSubview:[navigationController view]];
    [window addSubview:toolbar];
    [window makeKeyAndVisible];

    [rootViewController release];
    [aNavigationController release];

在 RootViewController 我使用这条指令传递给 DetailViewController:

//Push the new table view on the stack
    [self.navigationController pushViewController:detailsView animated:YES];
    [detailsView release];

DetailsViewController.h

#import <UIKit/UIKit.h>
#import <MessageUI/MessageUI.h>
#import "MLUtils.h"
#import <MediaPlayer/MediaPlayer.h>

@interface DetailsViewController : UIViewController 
    IBOutlet UILabel *titleLabel;
    IBOutlet UILabel *descriptionLabel;
    IBOutlet UIScrollView *descriptionScrollView;
    NSString *cityName;
    NSString *nomefile;
    NSString *extfile;
    NSString *description;


@property (nonatomic, retain) UILabel *titleLabel;
@property (nonatomic, retain) UILabel *descriptionLabel;
@property (nonatomic, retain) UIScrollView *descriptionScrollView;
@property (nonatomic, retain) NSString *cityName;
@property (nonatomic, retain) NSString *description;
@property (nonatomic, retain) NSString *nomefile;
@property (nonatomic, retain) NSString *extfile;

- (IBAction)playmovie:(id)sender;

@end

这是 DetailsViewController.m

   #import "DetailsViewController.h"

    @implementation DetailsViewController
    @synthesize titleLabel, descriptionLabel, descriptionScrollView;
    @synthesize cityName,description,nomefile, extfile;



   // Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
    - (void)viewDidLoad 
        [super viewDidLoad];
           [self.titleLabel setText:self.title];
           [self.descriptionLabel setText:self.description];

    float textHeight = [MLUtils calculateHeightOfTextFromWidth:self.description : descriptionLabel.font :descriptionLabel.frame.size.width :UILineBreakModeWordWrap];

        CGRect frame = descriptionLabel.frame;
        frame.size.height = textHeight;
        descriptionLabel.frame = frame;

        CGSize contentSize = descriptionScrollView.contentSize;
        contentSize.height = textHeight;
        descriptionScrollView.contentSize = contentSize;
    

-(void)playmovie:(id)sender
    

        NSString *appNomeFile = self.nomefile;
        NSString *appExtFile = self.extfile;

        NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:appNomeFile ofType:appExtFile]];

        MPMoviePlayerController *moviePlayer = 
        [[MPMoviePlayerController alloc] initWithContentURL:url];

        [[NSNotificationCenter defaultCenter] addObserver:self
                                                 selector:@selector(moviePlayBackDidFinish:)
                                                     name:MPMoviePlayerPlaybackDidFinishNotification
                                                   object:moviePlayer];

        moviePlayer.controlStyle = MPMovieControlStyleDefault;
        moviePlayer.shouldAutoplay = YES;


        [self.view addSubview:moviePlayer.view];

        [moviePlayer setFullscreen:YES animated:YES];
    

    - (void) moviePlayBackDidFinish:(NSNotification*)notification 

        MPMoviePlayerController *moviePlayer = [notification object];

        [[NSNotificationCenter defaultCenter] removeObserver:self      
                                                        name:MPMoviePlayerPlaybackDidFinishNotification
                                                      object:moviePlayer];

        if ([moviePlayer 
             respondsToSelector:@selector(setFullscreen:animated:)])
        
            [moviePlayer.view removeFromSuperview];
        
        [moviePlayer release];
    

    - (void)didReceiveMemoryWarning 
        // Releases the view if it doesn't have a superview.
        [super didReceiveMemoryWarning];

        // Release any cached data, images, etc that aren't in use.
    

    - (void)viewDidUnload 
        // Release any retained subviews of the main view.
        // e.g. self.myOutlet = nil;
    

    // Override to allow orientations other than the default portrait orientation.
    - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation 
        // Return YES for supported orientations
        //return (interfaceOrientation == UIInterfaceOrientationPortrait);
        return YES;
    

    - (void)dealloc 
        [titleLabel release];
        [descriptionLabel release];[descriptionScrollView release];
        [cityName release];
        [description release];
        [nomefile release];
        [extfile release];
        [super dealloc];
    

    @end

我的问题是:我的错误在哪里?我想它是在 playmovie 方法的调用中,但我无法找到解决方案!

附言 不小心把评论删了,很抱歉! =(

【问题讨论】:

【参考方案1】:

您似乎在类而不是对象上调用playmovie,或者您忘记提供参数。如果你告诉我们你叫它的地方,那可能会有所帮助。

无论如何,问题是你可能会这样做:

[DetailsViewController playmovie];

[oneDetailsViewController playmovie];

而不是:

[oneDetailsViewController playmovie:nil];

这里的oneDetailsViewController 是一个DetailsViewController* 对象。

编辑

删除您的 XIB 链接,保存并通过从按钮拖动(右键单击)到文件所有者再次创建指向 IBAction 的链接。

【讨论】:

playmovie 是一个动作!我通过单击 DetailsViewController.h 中 detailsviewcontroller 的 xib 上的按钮来调用它 - (IBAction)playmovie:(id)sender; 该死的,它有效!谢谢你! ____ 就是这样?为什么??? O___o 我有时真的感觉很糟糕! @obithemaster :我多次遇到同样的问题,所以现在...我知道 :-) 您可能已经链接了按钮,然后更改了方法名称/参数。所以 XCode 丢失了,但你总是可以在视觉上看到它......如果它对你有帮助,请不要忘记接受答案;-)

以上是关于NSInvalidArgumentException“发送到实例的无法识别的选择器”(使用 MPMoviePlayerController)的主要内容,如果未能解决你的问题,请参考以下文章

将 JSON 作为参数传递 swift