UISwipeGestureRecognizer使应用程序崩溃更改屏幕?

时间:2012-10-19 18:56:42

标签: iphone ios xcode ipad

我有一个简单的3页应用程序,我可以从第1页到第2页向左滑动。

然而,当我在第2页时,我需要添加能够转到第1页向右滑动,或第3页向左滑动,但它只是一直在崩溃。

我怀疑它可能是一个内存释放问题,但我想知道你是否可以如此友好地检查下面的代码是否存在任何明显的错误?

非常感谢,

- (void)viewDidLoad {

    UISwipeGestureRecognizer *swipeLeftRight = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleGesture:)];
    [swipeLeftRight setDirection:(UISwipeGestureRecognizerDirectionRight | UISwipeGestureRecognizerDirectionLeft )];
    [self.view addGestureRecognizer:swipeLeftRight];
    [UISwipeGestureRecognizer release];

}

- (IBAction)swipeLeftDetected:(UISwipeGestureRecognizer *)sender {


    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad){
        Page3ViewController *UIViewController =
        [[Page3ViewController alloc] initWithNibName:@"Page3ViewController~ipad" bundle:nil];
        [self presentModalViewController:UIViewController animated:YES];
          [Page2ViewController release];
    }else{

        Page3ViewController *UIViewController =
        [[Page3ViewController alloc] initWithNibName:@"Page3ViewController" bundle:nil];
        [self presentModalViewController:UIViewController animated:YES];
        [Page2ViewController release];
    }

        }


- (IBAction)swipeRightDetected:(UISwipeGestureRecognizer *)sender {


    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad){
        ViewController *UIViewController =
        [[ViewController alloc] initWithNibName:@"ViewController_iPad" bundle:nil];
        [self presentModalViewController:UIViewController animated:YES];

        [Page2ViewController release];

    }else{

        ViewController *UIViewController =
        [[ViewController alloc] initWithNibName:@"ViewController_iPhone" bundle:nil];
        [self presentModalViewController:UIViewController animated:YES];

        ViewController *VC = [[ViewController alloc] initWithNibName:@"ViewController" bundle:nil];
        [self presentModalViewController:VC animated:YES];

        [Page2ViewController release];

    }
}

2 个答案:

答案 0 :(得分:0)

编辑:

您需要实现一个名为handleGesture:的方法,因为这是您传递给手势识别器的操作。

答案 1 :(得分:0)

修正了,我发现了一个叫做rptwsthi的老帖子,我用了一些来解决它;

更改了viewDidLoad {

 UISwipeGestureRecognizer *rightRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(rightSwipeHandle:)];
    rightRecognizer.direction = UISwipeGestureRecognizerDirectionRight;
    [rightRecognizer setNumberOfTouchesRequired:1];
    [self.view addGestureRecognizer:rightRecognizer];
    [rightRecognizer release];

    //........towards left Gesture recogniser for swiping.....//
    UISwipeGestureRecognizer *leftRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(leftSwipeHandle:)];
    leftRecognizer.direction = UISwipeGestureRecognizerDirectionLeft;
    [leftRecognizer setNumberOfTouchesRequired:1];
    [self.view addGestureRecognizer:leftRecognizer];
    [leftRecognizer release];

用它来切换页面;

- (void)rightSwipeHandle:(UISwipeGestureRecognizer*)gestureRecognizer
{
    //Do moving

    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad){
        ViewController *UIViewController =
        [[ViewController alloc] initWithNibName:@"ViewController_iPad" bundle:nil];
        [self presentModalViewController:UIViewController animated:YES];

        [Page2ViewController release];

    }else{

        ViewController *UIViewController =
        [[ViewController alloc] initWithNibName:@"ViewController_iPhone" bundle:nil];
        [self presentModalViewController:UIViewController animated:YES];

        ViewController *VC = [[ViewController alloc] initWithNibName:@"ViewController" bundle:nil];
        [self presentModalViewController:VC animated:YES];

        [Page2ViewController release];

    }
}

- (void)leftSwipeHandle:(UISwipeGestureRecognizer*)gestureRecognizer
{
    // do moving

    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad){
        Page3ViewController *UIViewController =
        [[Page3ViewController alloc] initWithNibName:@"Page3ViewController~ipad" bundle:nil];
        [self presentModalViewController:UIViewController animated:YES];


    }else{

        Page3ViewController *UIViewController =
        [[Page3ViewController alloc] initWithNibName:@"Page3ViewController" bundle:nil];
        [self presentModalViewController:UIViewController animated:YES];

    }

}

不确定它在真正的编码器视图中有多整洁,但是它有效,有时你只需要使用正确的方法! :)