我创建了一个自定义 UIView,它从 UIViewController 实例化了 3 次。从viewDidLoad
那个 UIViewController 的方法:
self.remainingDays = [[RemainingTileView alloc] initWithFrame:CGRectMake(20, 49, 80, 75)];
self.remainingHours = [[RemainingTileView alloc] initWithFrame:CGRectMake(120, 49, 80, 75)];
self.remainingMinutes = [[RemainingTileView alloc] initWithFrame:CGRectMake(220, 49, 80, 75)];
[self.view addSubview:self.remainingDays];
[self.view addSubview:self.remainingHours];
[self.view addSubview:self.remainingMinutes];
在 RemainingTileView 类中,我有这个 layoutSubviews 方法:
- (void)layoutSubviews {
[super layoutSubviews];
if (self.number) // This is an NSNumber property
self.numberLabel = [self labelForNumber:[self.number intValue]];
else
self.numberLabel = [self labelForNumber:0];
if (self.unit) // This is an NSString property
self.unitLabel = [self labelForUnit:self.unit];
else
self.unitLabel = [self labelForUnit:@""];
[self configView];
}
创建视图时,它if (self.number)
与堆栈帧一起崩溃:
* thread #1: tid = 0x2403, 0x39f6c526 libobjc.A.dylib`objc_retain + 6, stop reason = EXC_BAD_ACCESS (code=1, address=0x10000010)
frame #0: 0x39f6c526 libobjc.A.dylib`objc_retain + 6
frame #1: 0x000dc742 myProject`-[RemainingTileView layoutSubviews](self=0x1e892b80, _cmd=0x344cde51) + 106 at RemainingTileView.m:63
frame #2: 0x3405d802 UIKit`-[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 258
frame #3: 0x33e07d8a QuartzCore`-[CALayer layoutSublayers] + 214
frame #4: 0x33e07928 QuartzCore`CA::Layer::layout_if_needed(CA::Transaction*) + 460
frame #5: 0x33e0885c QuartzCore`CA::Layer::layout_and_display_if_needed(CA::Transaction*) + 16
frame #6: 0x33e08242 QuartzCore`CA::Context::commit_transaction(CA::Transaction*) + 238
frame #7: 0x33e08050 QuartzCore`CA::Transaction::commit() + 316
frame #8: 0x33e07eb0 QuartzCore`CA::Transaction::observer_callback(__CFRunLoopObserver*, unsigned long, void*) + 60
frame #9: 0x322276cc CoreFoundation`__CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 20
frame #10: 0x322259c0 CoreFoundation`__CFRunLoopDoObservers + 276
frame #11: 0x32225d16 CoreFoundation`__CFRunLoopRun + 742
frame #12: 0x32198ebc CoreFoundation`CFRunLoopRunSpecific + 356
frame #13: 0x32198d48 CoreFoundation`CFRunLoopRunInMode + 104
frame #14: 0x35d6f2ea GraphicsServices`GSEventRunModal + 74
frame #15: 0x340ae300 UIKit`UIApplicationMain + 1120
frame #16: 0x000d3448 Project Countdown`main(argc=1, argv=0x2fd2ecf8) + 116 at main.m:17
self.number
是 NSNumber 的一个实例。正在从主线程修改 UI。我在 stackoverflow 上寻找了现有的解决方案,但没有任何效果。
我错过了什么?我应该寻找什么?