iPhone Swipe手势崩溃

时间:2011-02-20 23:22:17

标签: iphone objective-c uigesturerecognizer

我有一个应用程序,我希望滑动手势翻转到第二个视图。该应用程序都设置了可用的按钮。滑动手势会导致崩溃(“EXC_BAD_ACCESS”。)。

手势代码为:

- (void)handleSwipe:(UISwipeGestureRecognizer *)recognizer {
    NSLog(@"%s", __FUNCTION__);
    switch (recognizer.direction)
    {
        case (UISwipeGestureRecognizerDirectionRight):
            [self performSelector:@selector(flipper:)];
            break;

        case (UISwipeGestureRecognizerDirectionLeft): 
            [self performSelector:@selector(flipper:)];
            break;

        default:
            break;
    }   
}

and "flipper" looks like this:


- (IBAction)flipper:(id)sender {
    FlashCardsAppDelegate *mainDelegate = (FlashCardsAppDelegate *)[[UIApplication sharedApplication] delegate];
    [mainDelegate flipToFront];
}

flipToBack(和flipToFront)看起来像这样..

- (void)flipToBack {
     NSLog(@"%s", __FUNCTION__);

    BackViewController *theBackView = [[BackViewController alloc] initWithNibName:@"BackView" bundle:nil];
    [self setBackViewController:theBackView];
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:1.0];
    [UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft forView:window cache:YES];
    [frontViewController.view removeFromSuperview];
    [self.window addSubview:[backViewController view]];
    [UIView commitAnimations];
    [frontViewController release];
    frontViewController = nil;
    [theBackView release];
    //  NSLog (@" FINISHED ");
}

也许我会以错误的方式解决这个问题......欢迎所有想法......

2 个答案:

答案 0 :(得分:2)

您的选择器需要采用名称中:字符所暗示的参数,因此您应该使用performSelector:withObject:

[self performSelector:@selector(flipper:) withObject:nil];

答案 1 :(得分:2)

为什么你甚至使用performSelector:只是因为方法被标记为(IBAction)并没有使它与任何其他方法有任何不同,你可以将它们作为消息发送到类实例< / p>

- (void)handleSwipe:(UISwipeGestureRecognizer *)recognizer {
    NSLog(@"%s", __FUNCTION__);
    if ((recognizer.direction == UISwipeGestureRecognizerDirectionRight) || (recognizer.direction == UISwipeGestureRecognizerDirectionLeft)) {
        [self flipper:nil]
    }
}

实际上,由于手势方向只是位标志,因此可以写成:

- (void)handleSwipe:(UISwipeGestureRecognizer *)recognizer {
    NSLog(@"%s", __FUNCTION__);
    if (recognizer.direction & (UISwipeGestureRecognizerDirectionRight | UISwipeGestureRecognizerDirectionLeft)) {
        [self flipper:nil]
    }
}