分配 self.delegate 失败,它会产生无法识别的选择器发送
我是 iOS 新手,我在实现 @protocol 时遇到了麻烦,如果你认为这是一件简单的事情,我很抱歉。
我一直在 stackoverflow.com、网络上搜索,并尝试谷歌叔叔了一段时间,我决定在这里问...
主要思想是从 TopViewController 调用 MyViewController 并做翻转动画 我从创建协议开始..
// 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
下面是实现:
// 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 <UIScrollViewDelegate,MyViewControllerlDelegate>
{
UIScrollView *topScrollView;
UIButton *infoButton;
}
@end
TopViewController.h 实现 //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
下面的代码产生以下编译器错误。
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'
当我按下调用 -(void)infoIsTouch:(id)sender 方法的 infoButton 时,会引发这些错误,然后停在 [myView setDelegate:self] 行。任何人都可以给出给我一个提示,我的错误在哪里?
注意:如果你厌恶我的英语..也可以随意发表评论..但在此之前..我很抱歉我已经尽力了..
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..
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
旧帖子,但对于遇到相同问题的其他人,请确保将身份检查器中的类设置为正确的类。那是我的问题。可能是 Xcode 中最容易被遗忘的事情。
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.
你在某处
@synthesize delegate;
吗?哦,就在那里。德普。好的——所以——这是一个运行时错误。编译器警告等,见鬼(我的意思是——肯定要修复它们,但编译器正在生成正在运行和崩溃的代码)。
无法识别的选择器意味着尝试调用对象上的方法,但该对象不响应所述方法。在这种情况下,回溯不太可能有多大用处。您已经确定了崩溃位置以及崩溃原因。
尝试在崩溃的行上设置断点,然后使用调试器来询问该对象;
...等等...
可能有一些非常明显的事情正在发生,但我并没有立即看到它。
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;
... etc ...
There is something likely terribly obvious going on, but I don't see it offhand.
我的猜测是,这里还有一些名为
myView
的其他变量,它会拦截您对刚刚创建的MyViewController
实例的调用。尝试更改该实例的名称,看看会发生什么。其他内容:看起来您在
MyViewController
的实现中存在拼写错误。将此行:更改为:
您应该可以开始了。 (您收到该错误是因为由于拼写错误,头文件与任何实现都不对应,因此您永远无法从
@synthesize delegate;
调用中受益。另外,请确保您在
TopViewController
中声明正确的协议:并将协议声明更改为:
除此之外,一切看起来都不错。
My guess is that you have some other variable named
myView
here, that's intercepting your call to theMyViewController
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:to this:
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
:And change your protocol declaration to:
Other than that, things are looking good.