IOS:NavigationController开关UIView控制器,EXC_BAD_ACCESS EXC_I386_GPFLT错误

时间:2015-05-03 07:30:07

标签: ios objective-c uiviewcontroller

当我点击按钮将pushConController ViewController发送到ViewController2时, 第一次没问题,当我从ViewController2返回ViewController时, 第二次单击按钮到ViewController2, 发生错误:EXC_BAD_ACCESS EXC_I386_GPFLT

这是我的ViewController.m:

#import "ViewController.h"
#import "ViewController2.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.navigationController.delegate = self;
    self.circleAnimator = [MaterialCircleAnimator new];

    [self.button addTarget:self action:@selector(buttonClick:event:)     forControlEvents:UIControlEventTouchUpInside];

}

#pragma mark - UINavigationControllerDelegate iOS7新增的2个方法
- (id) navigationController:(UINavigationController     *)navigationController animationControllerForOperation:    (UINavigationControllerOperation)operation fromViewController:    (UIViewController *)fromVC toViewController:(UIViewController *)toVC
{
    if (operation == UINavigationControllerOperationPush) {
        return self.circleAnimator;
    }else if (operation == UINavigationControllerOperationPop){
        return self.circleAnimator;
    }else{ // UINavigationControllerOperationNone
        return nil;
    }
}

- (IBAction) buttonClick:(id)sender event:(UIEvent *)event
{
    CGPoint position = [[[event allTouches] anyObject] locationInView:sender];

    ViewController2 *secondViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"ViewController2"];

    //convert to absolute position
    position = [self.button convertPoint:position toView:self.view];

    [self.circleAnimator setCenterPoint:position];
    [self.navigationController pushViewController:secondViewController animated:YES];

}

@end

这是我的ViewController2.m:

#import "ViewController.h"
#import "ViewController2.h"

@implementation ViewController2

-(void)viewDidLoad
{
    self.navigationController.delegate = self;
    self.circleAnimator = [MaterialCircleAnimator new];
    [self.button addTarget:self action:@selector(clickBack:event:)     forControlEvents:UIControlEventTouchUpInside];
}

#pragma mark - UINavigationControllerDelegate iOS7新增的2个方法
- (id) navigationController:(UINavigationController *)navigationController animationControllerForOperation:(UINavigationControllerOperation)operation fromViewController:(UIViewController *)fromVC toViewController:(UIViewController *)toVC
{
    if (operation == UINavigationControllerOperationPush) {
        return self.circleAnimator;
    }else if (operation == UINavigationControllerOperationPop){
        return self.circleAnimator;
    }else{ // UINavigationControllerOperationNone
        return nil;
    }
}

- (IBAction)clickBack:(id)sender event:(UIEvent *)event
{
    CGPoint position = [[[event allTouches] anyObject] locationInView:sender];
    //convert to absolute position
    position = [self.button convertPoint:position toView:self.view];
    [self.circleAnimator setCenterPoint:position];

    [self.navigationController popViewControllerAnimated:YES];
}
@end

这是我在Github上的演示: https://github.com/TindleWei/MaterialCircleAnimation

1 个答案:

答案 0 :(得分:0)

每次在viewController2点按按钮时,您都不想创建viewController的新实例。您应该将其保留为viewController中的属性,并仅在其 nil 时对其进行实例化。因此secondViewController内的属性viewController的getter看起来像这样:

- (ViewController2 *)secondViewController {
  if (_secondViewController != nil) {
    return _secondViewController;
  }
  _secondViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"ViewController2"];
  return _secondViewController;
}