当我点击按钮将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
答案 0 :(得分:0)
每次在viewController2
点按按钮时,您都不想创建viewController
的新实例。您应该将其保留为viewController
中的属性,并仅在其 nil 时对其进行实例化。因此secondViewController
内的属性viewController
的getter看起来像这样:
- (ViewController2 *)secondViewController {
if (_secondViewController != nil) {
return _secondViewController;
}
_secondViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"ViewController2"];
return _secondViewController;
}