2010-12-20 56 views
0

Objective-C新手的位,我環顧四周尋找答案,但一直未能找到一個如此原諒我,如果這是一個明顯的問題。繼承UIView - 丟失實例變量

基本上,我需要繪製一個圓的屏幕段(例如,一個90度的段,其中水平線和垂直線在左下方相交併且一條圓弧連接終點)。我設法在一個名爲CircleSegment的自定義類中實現此功能,該類繼承UIView並覆蓋drawRect

我的問題是實現這個編程;我需要一些創建CircleSegment類的方法,並在之前存儲它所需的角度,它會繪製段本身。

這是我到目前爲止有:

CircleSegment.h

#import <UIKit/UIKit.h> 

@interface CircleSegment : UIView { 
    float angleSize; 
    UIColor *backColor; 
} 

-(float)convertDegToRad:(float)degrees; 
-(float)convertRadToDeg:(float)radians; 

@property (nonatomic) float angleSize; 
@property (nonatomic, retain) UIColor *backColor; 

@end 

CircleSegment.m

#import "CircleSegment.h" 


@implementation CircleSegment 

@synthesize angleSize; 
@synthesize backColor; 


// INITIALISATION OVERRIDES 
// ------------------------ 

- (id)initWithFrame:(CGRect)frame { 
    if ((self = [super initWithFrame:frame])) { 
     self.opaque = NO; 
     self.backgroundColor = [UIColor clearColor]; 
    } 
    return self; 
} 

- (void)setBackgroundColor:(UIColor *)newBGColor 
{ 
    // Ignore. 
} 
- (void)setOpaque:(BOOL)newIsOpaque 
{ 
    // Ignore. 
} 



// MATH FUNCTIONS 
// -------------- 

// Converts degrees to radians. 
-(float)convertDegToRad:(float)degrees { 
    return degrees * M_PI/180;  
} 

// Converts radians to degrees. 
-(float)convertRadToDeg:(float)radians { 
    return radians * 180/M_PI; 
} 


// DRAWING CODE 
// ------------ 

- (void)drawRect:(CGRect)rect { 

    float endAngle = 360 - angleSize; 

    UIBezierPath* aPath = [UIBezierPath bezierPathWithArcCenter:CGPointMake(100, 100) 
                 radius:100 
                startAngle:[self convertDegToRad:270] 
                 endAngle:[self convertDegToRad:endAngle] 
                 clockwise:YES]; 

    [aPath addLineToPoint:CGPointMake(100.0, 100.0)]; 

    [aPath closePath]; 

    CGContextRef aRef = UIGraphicsGetCurrentContext(); 
    CGContextSetFillColorWithColor(aRef, backColor.CGColor); 

    CGContextSaveGState(aRef); 

    aPath.lineWidth = 1; 

    [aPath fill]; 
    [aPath stroke]; 

    //CGContextRestoreGState(aRef); 


} 

- (void)dealloc { 
    [super dealloc]; 
} 


@end 

注意,.m文件是有點亂用各種測試代碼位...

所以基本上我想創建一個CircleSegment的實例,在angleSize屬性中存儲一個角度,根據該角度繪製一個線段,然後將該視圖添加到主應用視圖以顯示它...

嘗試而做到這一點,我已經添加下面的測試代碼viewDidLoad在我的ViewController:

CircleSegment *seg1 = [[CircleSegment alloc] init]; 
seg1.backColor = [UIColor greenColor]; 
seg1.angleSize = 10; 

[self.view addSubview:seg1]; 

看來存儲的UIColor和angleSize很好,當我斷點那些地方,但是如果我把一個斷點在drawRect我在CircleSegment.m上覆蓋,這些值已恢復爲零值(或任何正確的術語將是,請隨時糾正我)。

我真的很感激,如果有人能指出我在正確的方向!

謝謝

回答

0

看來存儲的UIColor和 angleSize很好,當我斷點那些 的地方,但是如果我把在drawRect中一個 斷點,我就CircleSegment.m 壓倒,在 值已歸還至零 值(或任何正確的術語 將是,請隨時糾正我)。

你確定你正在使用同一個實例嗎?

NSLog(@"%@ %p", NSStringFromSelector(_cmd), self);viewDidLoaddrawRect之類的東西下降,並確保你實際上是在處理同一個實例。

(我見過的情況下,開發商將alloc/initWithFrame:一個視圖,然後使用已NIB裝載過程中產生的,不知道爲什麼實例未正確初始化實例。)

+0

Aaaaaack,你完全正確。我會使用界面生成器將CircleSegment拖到我的ViewController上,並且我的斷點首先觸擊了_that_實例。這很煩人,你可能會錯過簡單的事情。所有的工作現在,謝謝! – Rich 2010-12-21 18:08:58

0

嘗試在DrawRect方法中使用self.angleSize(與backColor相同)。在您的測試方法中,將10.0替換爲10.0(以確保將其設置爲浮點數)。

另外,別忘了在你的dealloc中釋放backColor。

+0

感謝您的指針。雖然他們沒有解決問題,但我確實忘了發佈backColor。傻我! :) – Rich 2010-12-21 18:06:54