NSInvalidArgumentException “Unrecognized selector sent to instance” (using MPMoviePlayerController)

给你一囗甜甜゛ 提交于 2019-12-23 12:26:13

问题


Well, I have a TableView in a RootViewController with a DetailViewController for the display of the information of the single record. In the Detail page i have to play a multimedia file and i'm using the framework MediaPlayer, according to this guide: http://www.techotopia.com/index.php/Video_Playback_from_within_an_iOS_4_iPhone_Application

it seems all ok, but when i click on the play button i have this error:

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

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

These are my files:

In the AppDelegate I use this navigation controller:

[...]

// 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];

and in the RootViewController I use this instruction to pass to the 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

and this is the 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

My question is: where is my error ? I imagine it is in the call of the playmovie method, but I can't figure out a solution!

P.S.
I've accidentally erased a comment, I'm so sorry! =(


回答1:


You seem to call playmovie on a class and not on an object, or you forget to give the param. If you show us where you call it, that could help.

Anyway, the problem is that you probably do :

[DetailsViewController playmovie];

or

[oneDetailsViewController playmovie];

instead of :

[oneDetailsViewController playmovie:nil];

Here oneDetailsViewController is a DetailsViewController* object.

EDIT

Delete your XIB link, save, and make your link again to the IBAction by dragging (right-click) from the button to the file's owner.



来源:https://stackoverflow.com/questions/8151301/nsinvalidargumentexception-unrecognized-selector-sent-to-instance-using-mpmov

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