我正在从我的 Storyboard/iOS6/ARC 应用程序中的 xib 加载叠加 View 。这个 View 有一个 ImageView 、 TextView 和一些开关。我的问题是,在自定义一些控件之后,我在尝试从 View 转换回主视图时得到 EXC_BAD_ACCESS。
我通过从我的 View Controller 创建一个实例(在 button_touchup
函数期间)并调用 UIView transitionWithView
来加载 View .我可以初始化 imageView
和 text
通过填充这些控件的实例函数从 VC 传递数据。这工作正常,我可以毫无问题地过渡。
当我尝试自定义开关颜色时出现问题 - 例如_toggleSwitch.thumbTintColor = [UIColor colorwithRed.....];
切换显示为白色,然后在交互期间或 View 破坏时(当转换回主视图/self removeFromSuperview
时)与 bad_exec 一起崩溃。在使用实例函数转换到 View 之前,我试过这样做,我试过在 -awakeFromNib
上这样做和 -didMoveToWindow
没有运气。我试过将颜色保存在类(class)的强属性中。唯一可行的是使用内置常量 - 例如_toggleSwitch.thumbTintColor = [UIColor redColor]
.
如何在不崩溃的情况下自定义这些开关的外观?我什至尝试通过覆盖 View 的设置函数从我的呈现 View Controller 传递自定义 UIColor 对象,就像我将文本传递给 textfield.text
一样。和图像 imageview.image
- 通过将其分配给 self.toggleswitch.thumbTintColor
而且它仍然会导致糟糕的执行。如何自定义 xib 中的开关?
编辑:好的,我认为代码没有那么具有启发性,但这是叠加层
overlay.h
@property (strong, nonatomic) IBOutlet UISwitch *switchFB;
这在 -awakeFromNib/xib 中的任何其他地方都可以正常工作
Overlay.m
_switchFB.thumbTintColor = [UIColor redColor];
这会导致 exc_bad_access
_switchFB.thumbTintColor = [UIColor colorWithRed:225.0f green:152.0f blue:140.0f alpha:1.0f];
当我试图转换回 super View 时抛出异常(或者有时当与修改后的切换交互时):
- (IBAction)buttonCancelClick:(id)sender {
[UIView transitionWithView:self.superview
duration:0.5
options:UIViewAnimationOptionTransitionCurlUp
animations:^{
[self removeFromSuperview];
}
completion:nil
];
}
我认为这与对创建的 UIColor 对象的内存访问有关。我在访问传递到 Overlay 以初始化文本和 ImageView 的对象时没有问题,因此我修改了初始化这些对象的设置函数(从 parentVC 调用)以初始化我的开关。这仍然会崩溃。
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:堆栈跟踪 - 我回到 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
最佳答案
这是 UISwitch 特有的 SDK 中的错误。 UISwitch setThumbTintColor causing crash (iOS 6 only)?
在将 UIColor 函数变量分配给 nib 的 UISwitch thumbTintColor 属性之前调用 retain 可以解决崩溃问题。
关于ios - 初始化 NIB 负载控制 - EXC_BAD_ACCESS 销毁,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18035637/