xcode 为 UIView 获取“无法识别的选择器发送到实例”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9155343/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Getting "unrecognized selector sent to instance" For UIView
提问by Algorhythm
I have tried to cast an implementation of my UIView class to my view controller. However, when try to call a method for my class it throws "unrecognized selector sent to instance".
我试图将我的 UIView 类的实现转换为我的视图控制器。但是,当尝试为我的类调用方法时,它会抛出“无法识别的选择器发送到实例”。
Here is what I am doing in my header file:
这是我在头文件中所做的:
#import <UIKit/UIKit.h>
@interface GameViewController : UIViewController{
NSTimer *gameLoop;
}
- (IBAction)quitGame:(id)sender;
- (IBAction)rotateLeft:(id)sender;
- (IBAction)rotateRight:(id)sender;
- (IBAction)thrust:(id)sender;
@end
typedef enum { NOTREADY, READY, RUNNING, WON, LOST, PAUSED } GameState;
typedef enum { EASY, MEDIUM, HARD } GameDifficulty;
typedef enum { THRUSTERS_ON, THRUSTERS_OFF } ThrusterState;
// Declaration of other constants used to manage the physics
static const int FUEL_INITIAL = 200;
static const int FUEL_MAX = 200;
static const int FUEL_BURN = 10;
static const int MAX_INIT = 30;
static const int MAX_SPEED = 120;
static const int ACCELERATION_DOWN = 35;
static const int ACCELERATION_UP = 80;
static const double GRAVITY = 9.8;
@interface GameView : UIView {
@private UIImage *plander_thrust;
@private UIImage *plander_nothrust;
@private UIImage *plander_crashed;
// Other game member variables
@private GameState gstate;
@private GameDifficulty level;
@private ThrusterState thrusters;
@private int fuel;
@private int speed_x;
@private int speed_y;
@private double rotation;
// Define our lander's X and Y on-screen coordinates
@private int loc_x;
@private int loc_y;
}
@property (nonatomic, retain) UIImage *lander_nothrust;
// Declare our class methods
- (void) newGame;
- (void) updateLander;
- (void) rotateLeft:(id)sender;
- (void) rotateRight:(id)sender;
- (void) thrustEngine:(id)sender;
@end
Now, here is what I am doing with my .m file:
现在,这是我对 .m 文件所做的:
#import "GameView.h"
@implementation GameViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (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.
}
#pragma mark - View lifecycle
- (void)viewDidLoad
{
//'timerLoop' function
gameLoop = [NSTimer scheduledTimerWithTimeInterval: 0.025 target:self selector:@selector(timerLoop:) userInfo:nil repeats:YES];
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
- (void) timerLoop:(NSTimer *)timerObj {
//so this is where I am getting the error
[(GameView *)self.view updateLander];
[self.view setNeedsDisplay];
}
- (IBAction)quitGame:(id)sender {
[self dismissModalViewControllerAnimated:true];
}
- (IBAction)rotateLeft:(id)sender {
[(GameView *)self.view rotateLeft:sender];
}
- (IBAction)rotateRight:(id)sender {
[(GameView *)self.view rotateRight:sender];
}
- (IBAction)thrust:(id)sender {
[(GameView *)self.view thrustEngine:sender];
}
@end
@implementation GameView
@synthesize lander_nothrust;
-(id)initWithCoder:(NSCoder *)aDecoder{
if(self == [super initWithCoder:aDecoder]){
//initialize sprites
NSString *imagePath = [[NSBundle mainBundle] pathForResource:@"lander" ofType:@"tiff"];
self.lander_nothrust = [UIImage new];
self.lander_nothrust = [UIImage imageWithContentsOfFile:imagePath];
[self newGame];
}
return self;
}
-(void) newGame{
gstate = READY;
level = EASY;
thrusters = THRUSTERS_OFF;
fuel = FUEL_INITIAL;
loc_x = self.frame.size.width / 2;
loc_y = self.frame.size.height / 2;
// Set the game as RUNNING
gstate = RUNNING;
}
-(void)updateLander{
}
// rotateLeft - rotate the lander left
- (void)rotateLeft:(id)sender
{
// Do Something
}
// rotateRight - rotate the lander right
- (void)rotateRight:(id)sender
{
// Do Something
}
// thrustEngine - fire the thruster on the engine
- (void)thrustEngine:(id)sender
{
// Do Something
}
-(void)drawRect:(CGRect)rect{
if(gstate != RUNNING){
return;
}
[self.lander_nothrust drawAtPoint:CGPointMake(loc_x, loc_y)];
self.backgroundColor = [UIColor redColor];
}
@end
As shown above I am getting the error with this line of code:
如上所示,这行代码出现错误:
[(GameView *)self.view updateLander];
I have looked around and I keep hearing that it is because I am calling a method that doesn't exist for my object, but I am not sure why it is not seeing my object implementation. I have even set the custom class in for the UIView in the Identity Inspector to "GameView" as my class.
我环顾四周,我一直听说这是因为我正在调用一个对我的对象不存在的方法,但我不确定为什么它没有看到我的对象实现。我什至将身份检查器中 UIView 的自定义类设置为“GameView”作为我的类。
回答by rob mayoff
The problem is in your nib. Either you have not set the custom class of the view to GameView, or you have not connected the viewoutlet of File's Owner to that view.
问题出在你的笔尖上。您没有将视图的自定义类设置为GameView,或者您没有将viewFile's Owner的出口连接到该视图。

