2013-03-12 76 views
0

我創建了一個自定義的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上尋找現有的解決方案,但沒有任何工作。

我錯過了什麼?我應該尋找什麼?

+1

您的物業如何申報? – 2013-03-12 21:41:49

+0

你說得對。在此類的前一個迭代中,self.number是一個int。當我重構爲NSNumber時,@屬性被聲明爲'assign'。宣佈它「強」固定它。謝謝。你能否讓你的評論成爲答案,以便我可以接受它? – invalidArgument 2013-03-13 02:35:34

回答

3

看起來你的屬性有不正確的聲明。可能是assignweak而不是retain,copystrong

+0

實際上,由於內存管理不善,導致崩潰。它已經被修復了。所以,我正在刪除評論。 – Suran 2013-04-25 07:32:35

2

它看起來像屬性返回一些無效的對象和ARC試圖保留它,然後失敗。它看起來像你可能會更好的移動你的零值檢查到一個自定義屬性訪問器,沿着線的東西:

- (NSNumber *)number { 
    if (_number == nil) { 
     _number = [NSNumber numberWithInt:0]; 
    } 
    return _number; 
} 

然後,你可以簡單地設置numberLabel值號碼,而不必在這裏查看。你也可以爲unit做類似的事情。

+0

謝謝你的回答。我投了你的答案,並且我應用了你的解決方案。你也指出了正確的方向,但@Jonathan Grynspan的評論是真實的。 – invalidArgument 2013-03-13 02:34:51

相關問題