I am trying to call a new view inside my existing view
TransactionFinish *childView= [[TransactionFinish alloc] initWithNibName:@"TransactionFinish" bundle:nil];
childView.view.frame = self.view.frame;
childView.view.frame=CGRectMake(10, 10, self.view.frame.size.width-20, self.view.frame.size.height-20);
childView.view.alpha = 0.0f;
[self.view addSubview:childView.view];
[UIView animateWithDuration:0.5 delay:0.0 options:UIViewAnimationCurveEaseOut
animations:^{
childView.view.alpha = 1.0f;
}
completion:^(BOOL finished) {
}];
It is going inside ViewDidLoad() of TransactionFinish(I have tried debugging it) but it gives me Thread EXEC_BAD_ACCESS(code=1,address=0x31f54e62) with green color
The issue lays with the lifetime of
childView. You instantiate and store a reference to it into a local variable:If you are using ARC, when the local variable goes out of scope, the object referenced by it (
childView) is deallocated.If you are not using ARC, I suppose you are doing:
somewhere to avoid
childViewto be leaked (as the code you pasted above would imply).Either hypothesis would explain why you get the crash: when
viewDidLoadis called, the controller has already been deallocated.Adding
childViewview toself.view:will retain
childView.viewbut notchildView. So the controller is deallocated, while its view is not.A fix to this is creating a
strongproperty in your class to store a reference to yourchildViewcontroller:Another possibility is using controller containment; you could do something li
but this will only work on iOS5+.