问题
I have two view controllers, one is the home view, one is the side menu view; all of the objects and created with storyboard.
there is a bar button 'item' on home view, when clicking it, it will invoke a custom segue, to slide out the side menu from left to right. So far so good.
On side menu, there is a button to slide back to home view. However, I simply click the button, the app will terminated, Xcode does not print out the crash log overtime.
I tried to add a breakpoint on (IBAction)SlideBack:(id)sender
, seems like it did not get invoked. So something series happened.
Xcode break at
return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));
and saying Thread 1: exc_bad_access(code=EXC_i386_GPFLT)
I suspect the custom segue may interfere the view and view controllers hierarchy. But could not find out.
home view implementation:
@implementation HomeViewController
@end
side bar implementation:
@implementation SideBarController
- (IBAction)SlideBack:(id)sender {
NSLog(@"sliding back");
}
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
The custom slide out segue is:
@implementation CustomSegue
- (void)perform {
UIViewController *contentVC = self.sourceViewController;
UIViewController *sideBarVC = self.destinationViewController;
CGRect sideFrame = sideBarVC.view.frame;
[sideBarVC.view setFrame: CGRectMake(-(contentVC.view.frame.size.width),
0,
contentVC.view.frame.size.width/2,
contentVC.view.frame.size.height)];
CGRect animationFrame = contentVC.view.frame;
sideFrame = sideBarVC.view.frame;
animationFrame.size.width = contentVC.view.frame.size.width / 2;
sideBarVC.view.alpha = 0;
[[[contentVC.view superview] window] addSubview:sideBarVC.view];
[UIView animateWithDuration:1
delay:0.0
options:UIViewAnimationOptionCurveEaseOut
animations:^{
sideBarVC.view.frame = animationFrame;
sideBarVC.view.alpha = 1;
}
completion:^(BOOL finished) {
sideBarVC.view.alpha = 1;
sideBarVC.view.frame = animationFrame;
}];
}
回答1:
After seeking helps, I found I misunderstand the segue logic.
[[[contentVC.view superview] window] addSubview:sideBarVC.view];
is not enough.
Instead, below code should be right:
[contentVC addChildViewController:sideBarVC];
[[[contentVC.view superview] window] addSubview:sideBarVC.view]
[sideBarVC didMoveToParentViewController:contengVC];
来源:https://stackoverflow.com/questions/27959170/tap-button-cause-app-crash-on-side-bar