0

我有一个简单的 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];

    }
}
4

2 回答 2

0

编辑:

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

于 2012-10-19T19:04:04.047 回答
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];

    }

}

不确定在真正的编码人员看来它有多整洁,但它确实有效,有时你只需要选择正确的方法!:)

于 2012-10-20T01:24:34.460 回答