我創建了一個自定義的UIView,它從UIViewController實例化了3次。從UIViewController中的viewDidLoad
方法:自定義UIView在layoutSubviews中導致EXC_BAD_ACCESS代碼= 1
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上尋找現有的解決方案,但沒有任何工作。
我錯過了什麼?我應該尋找什麼?
您的物業如何申報? – 2013-03-12 21:41:49
你說得對。在此類的前一個迭代中,self.number是一個int。當我重構爲NSNumber時,@屬性被聲明爲'assign'。宣佈它「強」固定它。謝謝。你能否讓你的評論成爲答案,以便我可以接受它? – invalidArgument 2013-03-13 02:35:34