无法分配self.delegate它会产生无法识别的选择器

我是iOS的新手,我在实现@protocol方面遇到了麻烦,如果你觉得这很容易,我很抱歉。

我一直在搜索stackoverflow.com,网站,并尝试谷歌叔叔一段时间,我决定在这里问…

主要的想法是从TopViewController调用MyViewController并执行翻转动画我开始创建协议。

// This is my Delegate header // MyViewController.h @protocol MyViewControllerlDelegate - (void) myViewControllerDidFinish; @end @interface MyViewController : UIViewController { id  delegate; } @property (nonatomic, assign) id  delegate; - (IBAction) done; @end 

以下是实施:

  // MyViewController.m #import "MyViewController.h" @implementation MyViewController @synthesize delegate; // Another Code above - (IBAction)done { [self.delegate myViewControllerDidFinish]; } // Another Code Below @end 

我使用上面的对象从下面的另一个视图调用并在其中添加翻转过渡:

 // TopViewController.h #import "MyViewController.h" @class MapScrollView; @interface TopViewController : UIViewController  { UIScrollView *topScrollView; UIButton *infoButton; } @end 

TopViewController.h实现//TopViewController.m

 #import "TopViewController.h" #import "CustomScrollView.h" #import  - (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 

下面的代码产生以下编译错误..

 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' 

当我按下infoButton调用 – (void)infoIsTouch:(id)sender方法,然后停在[myView setDelegate:self]行时,会出现这些错误。任何人都可以给我一个提示,我的错误在哪里?

注意:如果你厌恶我的英语..随意评论也可以..但在那之前…我很抱歉我尽我所能..

我的猜测是你在这里有一些名为myView变量,它会拦截你刚刚创建的MyViewController实例的调用。 尝试更改该实例的名称,看看会发生什么。

其他的东西:像你在MyViewController的实现中有一个拼写错误。 改变这一行:

 @implementation FanglesMapSettingsViewController 

对此:

 @implementation MyViewController 

你应该好好去。 (您收到该错误是因为头文件与任何实现不对应,因为打字错误,所以您永远不会得到@synthesize delegate;的好处@synthesize delegate;请致电。

另外,请确保在TopViewController声明正确的协议:

 @interface TopViewController : UIViewController  

并将您的协议声明更改为:

 @protocol MyViewControllerlDelegate 

除此之外,事情看起来很好。

旧post,但对于具有相同问题的任何其他人,请确保将身份检查器中的类设置为正确的类。 那是我的问题。 可能是Xcode中最被遗忘的事情。

你是@synthesize delegate;吗? 某处? 哦,就是这样。 DERP。

好的 – 所以 – 这是一个运行时错误。 编译器警告等应该被诅咒(我的意思是 – 确定它们,但是编译器正在生成运行和崩溃的代码)。

无法识别的选择器意味着尝试在对象上调用方法,该对象不响应所述方法。 在这种情况下,回溯不太可能非常有用; 您已经确定了崩溃位置以及崩溃的原因。

尝试在崩溃的行上设置断点,然后使用调试器查询所述对象;

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

……等……

有一些事情可能非常明显,但我不会随便看到它。