在初始化另一个对象中的UISwipeGestureRecognizer时,使用未声明的标识符“action”

时间:2013-12-30 02:37:24

标签: ios objective-c ios7 uigesturerecognizer sprite-kit

我试图在我的ViewController的viewDidLoad中创建一个UISwipeGestureRecognizer,它将使用一个选择器来调用另一个对象(MyScene)中的方法。但是,我收到以下错误:

  

使用未声明的标识'行动'

这是不是有原因?

示例代码:

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];

// Configure the view.
SKView * skView = (SKView *)self.view;
if (!skView.scene) {
  skView.showsFPS = YES;
  skView.showsNodeCount = YES;

  // Create and configure the scene.
  SKScene * scene = [MyScene sceneWithSize:skView.bounds.size];
  scene.scaleMode = SKSceneScaleModeAspectFill;
  // Present the scene.
  [skView presentScene:scene];

    UISwipeGestureRecognizer *swipeRight = [[UISwipeGestureRecognizer alloc] initWithTarget:(MyScene) action:@selector(screenSwipedRight)];
    swipeRight.numberOfTouchesRequired = 1;
    swipeRight.direction=UISwipeGestureRecognizerDirectionRight;
    [self.view addGestureRecognizer:(swipeRight)];

    UISwipeGestureRecognizer *swipeLeft = [[UISwipeGestureRecognizer alloc] initWithTarget:(MyScene) action:@selector(screenSwipedLeft)];
    swipeLeft.numberOfTouchesRequired = 1;
    swipeLeft.direction=UISwipeGestureRecognizerDirectionLeft;
    [self.view addGestureRecognizer:(swipeLeft)];

    UISwipeGestureRecognizer *swipeUp = [[UISwipeGestureRecognizer alloc] initWithTarget:(MyScene) action:@selector(screenSwipedUp)];
    swipeUp.numberOfTouchesRequired = 1;
    swipeUp.direction=UISwipeGestureRecognizerDirectionUp;
    [self.view addGestureRecognizer:(swipeUp)];
    }
}

如果需要更多细节,我很乐意提供任何其他信息。

感谢您抽出宝贵时间提供帮助!

2 个答案:

答案 0 :(得分:2)

目标需要是类的实例:

UISwipeGestureRecognizer *swipeRight = [[UISwipeGestureRecognizer alloc] initWithTarget:(MyScene) action:@selector(screenSwipedRight)];

可能需要:

UISwipeGestureRecognizer *swipeRight = [[UISwipeGestureRecognizer alloc] initWithTarget: scene action:@selector(screenSwipedRight)];

Merhod screenSwipedRight应该是类MyScene的publia方法。

答案 1 :(得分:1)

你的initWithTarget方法都说initWithTarget:(MyScene)。我认为你想改用self

如果您希望场景对象接收事件,请使用:

UISwipeGestureRecognizer *swipeUp = [[UISwipeGestureRecognizer alloc] initWithTarget:scene action:@selector(screenSwipedUp)];