简体   繁体   中英

iPhone Swipe Gesture crash

I have an app that I'd like the swipe gesture to flip to a second view. The app is all set up with buttons that work. The swipe gesture though causes a crash ( “EXC_BAD_ACCESS”.).

The gesture code is:

- (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 (and flipToFront) look like this..

- (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 ");
}

Maybe I'm going about this the wrong way... All ideas are welcome...

您的选择器需要接受名称中的:字符所隐含的参数,因此您应该使用performSelector:withObject:

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

Why are you even using performSelector: Just because a method is marked as an (IBAction) doesn't make it any different from any other method, and you can send them as messages to a class instance

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

Actually, since the gesture directions are just bit flags this can be written as:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM