2015-05-03 30 views
0

當我點擊按鈕pushViewController ViewControllerViewController2, 第一次是好的,當我從ViewController2返回ViewController, 第二次點擊按鈕ViewController2, 發生errer:EXC_BAD_ACCESS EXC_I386_GPFLTIOS:NavigationController開關UIViewcontrollers,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

回答

0

你不想要每次在viewController中點擊該按鈕時創建viewController2的新實例。您應該將其保留爲viewController中的一個屬性,並僅在其時將其實例化。因此viewController內的secondViewController屬性的獲取方式如下所示:

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

問題已修復。我不是爲什麼簡單的常規方式不能工作,但它運作良好。無論如何。 – AdamWei