I'm loading an overlay view from an xib in my storyboard / iOS6 / ARC app. This view has an image view, text view and some switches. My problem is that after customizing some of the controls, I get EXC_BAD_ACCESS when trying to transition away from the view back to my main view.
I'm loading the view by creating an instance from my view controller (during a button_touchup
function) and calling UIView transitionWithView
. I can initialize the imageView
and text
with data passed from the VC through an instance function that populates those controls. This works fine and I can transition away no problem.
The problem comes in when I try to customize the switch colors - e.g. _toggleSwitch.thumbTintColor = [UIColor colorwithRed.....];
The toggle shows up white and then crashes with bad_exec during interaction or on destruction of the view (when transitioning back to the main view / self removeFromSuperview
). I've tried doing this before I transition to the view with an instance function, I've tried doing it on -awakeFromNib
and -didMoveToWindow
without luck. I've tried saving the colors in strong properties on the class. The only thing that works is using a built in constant - e.g. _toggleSwitch.thumbTintColor = [UIColor redColor]
.
How can I customize the appearance of these switches without crashing? I even tried passing in the custom UIColor object from my presenting view controller through the overlay view's setup function the same way I'm passing in the text to textfield.text
and the image to imageview.image
- by assigning it to self.toggleswitch.thumbTintColor
and it still causes a bad exec. How do I customize the switches in my xib?
EDIT:Ok, I didn't think the code was all that revelatory, but here's the overlay
overlay.h
@property (strong, nonatomic) IBOutlet UISwitch *switchFB;
This works fine in -awakeFromNib / anywhere else in the xib
Overlay.m
_switchFB.thumbTintColor = [UIColor redColor];
This causes exc_bad_access
_switchFB.thumbTintColor = [UIColor colorWithRed:225.0f green:152.0f blue:140.0f alpha:1.0f];
The exception is thrown when I'm trying to transition back to the superview (or sometimes when interacting with the modified toggle):
- (IBAction)buttonCancelClick:(id)sender {
[UIView transitionWithView:self.superview
duration:0.5
options:UIViewAnimationOptionTransitionCurlUp
animations:^{
[self removeFromSuperview];
}
completion:nil
];
}
I figured it has to do with memory access to the created UIColor object. I don't have a problem accessing objects I pass into the Overlay to initialize the text an image views, so I modifed the setup function (called from the parentVC) that initializes those objects to initialize my switch as well. This still crashes.
Overlay.h
@property (strong, nonatomic) IBOutlet UITextView *textField;
@property (strong, nonatomic) IBOutlet UIImageView *imageView;
Overlay.m
- (void)setup:(UIImage *)img text:(NSString *)txt color:(UIColor *)col
{
self.switchFB.thumbTintColor = col;
self.textField.text = txt;
self.imageView.image = img;
}
ParentVC.m
OverlayPublish *olay = [[NSBundle mainBundle] loadNibNamed:@"OverlayPublish"owner:self options:nil][0];
[olay setup:[UIImage imageNamed:@"test.png"] text:@"test txt" color:[UIColor colorWithRed:225.0f green:152.0f blue:140.0f alpha:1.0f]];
[UIView transitionWithView:self.view.superview.superview
duration:0.5
options:UIViewAnimationOptionTransitionCurlDown
animations:^{
[self.view.superview.superview addSubview:olay];
}
completion:nil
Edit2: stack trace - I went back to removeFromSuperView.
thread #1: tid = 0x1c03, 0x01d8209b libobjc.A.dylib`objc_msgSend + 15, stop reason = EXC_BAD_ACCESS (code=1, address=0x51019e21)
frame #0: 0x01d8209b libobjc.A.dylib`objc_msgSend + 15
frame #1: 0x0224d41c CoreFoundation`CFRelease + 108
frame #2: 0x02272e54 CoreFoundation`-[__NSArrayM dealloc] + 196
frame #3: 0x01d849ff libobjc.A.dylib`-[NSObject release] + 47
frame #4: 0x01d73927 libobjc.A.dylib
ReleaseValue std::for_each<__gnu_cxx::__normal_iterator<objc_references_support::ObjcAssociation*, std::vector<objc_references_support::ObjcAssociation, objc_references_support::ObjcAllocator<objc_references_support::ObjcAssociation> > ,ReleaseValue>(__gnu_cxx::__normal_iterator<objc_references_support::ObjcAssociation*, std::vector<objc_references_support::ObjcAssociation, objc_references_support::ObjcAllocator<objc_references_support::ObjcAssociation> > >, __gnu_cxx::__normal_iterator<objc_references_support::ObjcAssociation*, std::vector<objc_references_support::ObjcAssociation, objc_references_support::ObjcAllocator<objc_references_support::ObjcAssociation> > >, ReleaseValue) + 72 frame #5: 0x01d73632 libobjc.A.dylib
_object_remove_assocations + 296frame #6: 0x01d7a7aa libobjc.A.dylib`objc_destructInstance + 60
frame #7: 0x01d7a7cf libobjc.A.dylib`object_dispose + 20
frame #8: 0x00b2601a UIKit`-[UIImage dealloc] + 217
frame #9: 0x01d849ff libobjc.A.dylib`-[NSObject release] + 47
frame #10: 0x00c0df90 UIKit`-[UIImageView dealloc] + 752
frame #11: 0x00b4199c UIKit`-[UIView release] + 93
frame #12: 0x00b497fb UIKit`-[UIView(Hierarchy) removeFromSuperview] + 190
frame #13: 0x00b43ee5 UIKit`-[UIView dealloc] + 375
frame #14: 0x00d06a01 UIKit`-[_UISwitchInternalView dealloc] + 288
frame #15: 0x00b4199c UIKit`-[UIView release] + 93
frame #16: 0x00b497fb UIKit`-[UIView(Hierarchy) removeFromSuperview] + 190