簡體   English   中英

獲取“無法識別的選擇器發送到實例”對於UIView

[英]Getting “unrecognized selector sent to instance” For UIView

我試圖將我的UIView類的實現強制轉換為我的視圖控制器。 但是,當嘗試為我的類調用方法時,它會拋出“無法識別的選擇器發送到實例”。

這是我在頭文件中所做的事情:

#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

現在,這是我正在使用我的.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

如上所示我得到這行代碼的錯誤:

[(GameView *)self.view updateLander];

我環顧四周,我一直聽說這是因為我正在調用一個對我的對象不存在的方法,但我不確定為什么它沒有看到我的對象實現。 我甚至在Identity Inspector中將UIView的自定義類設置為“GameView”作為我的類。

問題出在你的筆尖上。 您沒有將視圖的自定義類設置為GameView ,或者您沒有將文件所有者的view插座連接到該視圖。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM