OGeek|极客世界-中国程序员成长平台

标题: ios - 初始化 NIB 加载控件 - EXC_BAD_ACCESS 销毁 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-12 13:21
标题: ios - 初始化 NIB 加载控件 - EXC_BAD_ACCESS 销毁

我正在从我的 Storyboard/iOS6/ARC 应用程序中的 xib 加载覆盖 View 。这个 View 有一个 ImageView 、 TextView 和一些开关。我的问题是,在自定义了一些控件之后,当我试图从 View 转换回我的主视图时,我得到了 EXC_BAD_ACCESS。

我通过从我的 View Controller 创建一个实例(在 button_touchup 函数期间)并调用 UIView transitionWithView 来加载 View 。我可以使用从 VC 通过填充这些控件的实例函数传递的数据来初始化 imageViewtext。这很好用,我可以毫无问题地过渡。

当我尝试自定义开关颜色时出现问题 - 例如_toggleSwitch.thumbTintColor = [UIColor colorwithRed.....]; 切换显示为白色,然后在交互过程中或 View 破坏时与 bad_exec 一起崩溃(转换回主视图时/self removeFromSuperview)。在使用实例函数转换到 View 之前,我已经尝试过这样做,我已经尝试在 -awakeFromNib-didMoveToWindow 上执行此操作,但没有运气。我已经尝试将颜色保存在类的强属性中。唯一有效的是使用内置常量 - 例如_toggleSwitch.thumbTintColor = [UIColor redColor]

如何在不崩溃的情况下自定义这些开关的外观?我什至尝试通过覆盖 View 的设置函数从我的呈现 View Controller 传递自定义 UIColor 对象,就像我将文本传递给 textfield.text 和图像传递给 imageview 一样。图像 - 通过将其分配给 self.toggleswitch.thumbTintColor 并且它 still 会导致错误的执行。如何自定义我的 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)buttonCancelClickid)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)setupUIImage *)img textNSString *)txt colorUIColor *)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 addSubviewlay];
                }
                completion:nil

Edit2:堆栈跟踪 - 我回到 removeFromSuperView。



Best Answer-推荐答案


这是特定于 UISwitch 的 SDK 中的错误。 UISwitch setThumbTintColor causing crash (iOS 6 only)?

在将 UIColor 函数变量分配给 nib 的 UISwitch thumbTintColor 属性之前调用它可以解决崩溃问题。

关于ios - 初始化 NIB 加载控件 - EXC_BAD_ACCESS 销毁,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18035637/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://ogeek.cn/) Powered by Discuz! X3.4