The property delegate of CALayer cause Crash.
生活随笔
收集整理的這篇文章主要介紹了
The property delegate of CALayer cause Crash.
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
在非arc與arc的環境中使用時,導致程序crash.
?
1 //當前類使用-fno-objc 編譯 2 3 needle_ = [[MyView alloc] init]; 4 5 needleLayer = [CALayer layer]; 6 needleLayer.delegate = self.needle; 7 8 9 - (void)dealloc 10 { 11 [needle_ release]; 12 [needleLayer release]; 13 [super dealloc]; //crash 14 } 15?
?
1 -(void) drawLayer: (CALayer*) layer inContext: (CGContextRef) context?
he lightest-wight solution would be to create a small helper class in the the file as the UIView that's using the CALayer:
In MyView.h
@interfaceMyLayerDelegate:NSObject...@endIn MyView.m
@implementationMyLayerDelegate-(void)drawLayer:(CALayer*)layer inContext:(CGContextRef)ctx {...}@endJust place those at the top of your file, immediately below the #import directives. That way it feels more like using a "private class" to handle the drawing (although it isn't -- the delegate class can be instantiated by any code that imports the header).
?
Feedback #1樓 2012-11-09 11:51?|?為了誰?? needleLayer.delegate = nil;這句話寫在dealloc中,因為CALayer中的delegate是assign的屬性,這樣你釋放[needle_ release];后,deletate就是野指針了。在arc中如果有weak了,如果delegatej是weak的屬性,就不會crash..我們在程序設計中也要注意傳指針。 支持(0)反對(0)
??回復引用刪除 #2樓[樓主] 2012-11-14 15:07?|?不及格的程序員-八神?? @?為了誰
請教一下,為什么視圖已經在delloc中了,它還會訪問用CALayer的delegate呢? 支持(0)反對(0)
?
總結
以上是生活随笔為你收集整理的The property delegate of CALayer cause Crash.的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 人生只有七次机会,你抓住了几次?
- 下一篇: 如何在XenDesktop没有工具栏情况