Fail in assigning self.delegate it produce unrecog

2019-06-24 01:14发布

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..

3条回答
何必那么认真
2楼-- · 2019-06-24 02:00

Do you @synthesize delegate; somewhere? Oh, there it is. Derp.

OK -- so -- this is a runtime error. Compiler warnings, etc, be damned (I mean -- fix 'em, definitely, but the compiler is generating code that is running and crashing).

An unrecognized selector means that an attempt was made to invoke a method on an object where that object doesn't respond to said method. In this case, the backtrace isn't likely to be terribly useful; you already identified the crash location and why it is crashing.

Try setting a breakpoint on the line that is crashing and then use the debugger to interrogate said object;

po object
po (id) [object class]
p (BOOL) [object respondsToSelector: @selector(setDelegate:)]

... etc ...

There is something likely terribly obvious going on, but I don't see it offhand.

查看更多
狗以群分
3楼-- · 2019-06-24 02:01

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.

查看更多
放荡不羁爱自由
4楼-- · 2019-06-24 02:16

My guess is that you have some other variable named myView here, that's intercepting your call to the MyViewController instance you've just created. Try changing the name of that instance, and see what happens.

Other stuff: ooks like you have a typo in your implementation of MyViewController. Change this line:

@implementation FanglesMapSettingsViewController

to this:

@implementation MyViewController

And you should be good to go. (You're getting that error because the header file doesn't correspond to any implementation, due to the typo, so you never get the benefit of your @synthesize delegate; call.

Also, make sure you declare the proper protocol in TopViewController:

@interface TopViewController : UIViewController <UIScrollViewDelegate, MyViewControllerlDelegate> 

And change your protocol declaration to:

@protocol MyViewControllerlDelegate<NSObject>

Other than that, things are looking good.

查看更多
登录 后发表回答