Fail in assigning self.delegate it produce unrecognized selector sent

FerryHtw picture FerryHtw · Dec 5, 2010 · Viewed 11.8k times · Source

I'm a new in iOS, and I have trouble in implementing @protocol so sorry if you think this is an easy thing..

i've been searching around stackoverflow.com, the webs and also try uncle Google for a while and I decided to ask here...

The main idea is calling a MyViewController from TopViewController and do flip animation I Start with creating the protocols..

    // This is my Delegate header
    // MyViewController.h

    @protocol MyViewControllerlDelegate

    - (void) myViewControllerDidFinish;

    @end

    @interface MyViewController : UIViewController 
    {
     id <MyViewControllerlDelegate> delegate;
    }

    @property (nonatomic, assign) id <MyViewControllerlDelegate> delegate;

    - (IBAction) done;

    @end

and below is on implementation :

    // MyViewController.m

    #import "MyViewController.h"

    @implementation MyViewController

    @synthesize delegate;

    // Another Code above

    - (IBAction)done 
    {
     [self.delegate myViewControllerDidFinish];
    }

    // Another Code Below

    @end

I use above object to be called from another view below and add flip transition in it :

// TopViewController.h

#import "MyViewController.h"

@class MapScrollView;

@interface TopViewController : UIViewController <UIScrollViewDelegate,MyViewControllerlDelegate> 
{
    UIScrollView *topScrollView;
    UIButton *infoButton;
}

@end

TopViewController.h Implementation //TopViewController.m

#import "TopViewController.h"
#import "CustomScrollView.h"
#import <QuartzCore/QuartzCore.h>

- (void)loadView 
{    
    // Step 1: make the outer paging scroll view
    CGRect topScrollViewRect = [[UIScreen mainScreen] bounds];
    topScrollView = [[UIScrollView alloc] initWithFrame:topScrollViewRect];
    topScrollView.pagingEnabled = YES;
    topScrollView.backgroundColor = [UIColor blackColor];
    topScrollView.showsVerticalScrollIndicator = NO;
    topScrollView.showsHorizontalScrollIndicator = NO;
    topScrollView.contentSize = CGSizeMake(topScrollViewRecte.size.width,
                                              topScrollViewRecte.size.height);
    topScrollView.delegate = self;
    self.view = pagingScrollView;

    CustomScrollView *sView = [[[MapScrollView alloc] init] autorelease];

    sView.frame = [[UIScreen mainScreen] bounds];

    [sView displayTiledMap];

    [pagingScrollView addSubview:sView];

    // add Info Button
    UIButton *button = [UIButton buttonWithType:UIButtonTypeInfoDark];
    [button addTarget:self action:@selector(infoIsTouch:)   forControlEvents:UIControlEventTouchDown];
    button.frame = CGRectMake(282.0, 440.0, 18.0, 19.0);
    [self.view addSubview:button];
}

-(void)infoIsTouch:(id)sender
{
 MyViewController *myView = 
 [[MyViewController alloc] initWithNibName:@"MyViewController" bundle:[NSBundle mainBundle]];
 myView.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
        //
        //Here where the code is crash 
        //
 [myView setDelegate:self];
        //
        //Code from here to below is not run...
        //
 [self presentModalViewController:myView animated:YES];
 [myView release];
}

- (void) myViewControllerDidFinish
{
 [self dismissModalViewControllerAnimated:YES];
}

etc... 

@end

Below code produce following compiler error..

2010-12-06 01:09:07.946 MyViewDelegatePractice[9473:207] -[TopViewController setDelegate:]: unrecognized selector sent to instance 0x5f30fb0
2010-12-06 01:09:07.949 MyViewDelegatePractice[9473:207] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[TopViewController setDelegate:]: unrecognized selector sent to instance 0x5f30fb0'
*** Call stack at first throw:
(
    // Cuts... 
)
terminate called after throwing an instance of 'NSException'

Those error is raised when i press infoButton which call -(void)infoIsTouch:(id)sender method, then stop at [myView setDelegate:self] line.. could anyone give me a hint where is my mistakes is ?

NOTE : If you disgust with my english.. fell free to comment abut that also.. but before that.. i'm sorry i've try my best..

Answer

jdog picture jdog · May 17, 2012

Old post, but for anyone else with the same issue make sure you set the Class in the identity inspector to the correct Class. That was my issue. Probably the most forgotten thing to do in Xcode.