2013-11-01 40 views
0

編輯:問題已解決,我將變量的分配和啓動移動到另一個方法中。 SubOtherClass永遠不會啓動(從不會調用allocinit)。方法調用時不運行

類已重命名,使此問題更一般化。
假設類OtherClass擴展NSView
假設類SubOtherClass擴展假想類OtherClass並調用的ClassToUpdate 本地實例更新方法據我所知,在更新時一鍵被釋放的觀點是不是最好的想法,但這只是臨時。我不是Obj-C的專家。若要重複此問題,將執行SubOtherClass中的更新方法,但不會在ClassToUpdate中執行,並且該方法的內容(此處未顯示)不會運行。我怎樣才能解決這個問題?如果有需要的話,請詢問。

謝謝。

編輯:全碼(用重命名類)

頁眉:

#import "OtherClass.h" 
#import "ThingToRender.h" 
#import "ClassToUpdate.h" 

@interface SubOtherClass : OtherClass 

@property (assign) ThingToRender *thingToRender1, *thingToRender2; 
@property (retain) ClassToUpdate *classToUpdate; 

- (void) createVariables; 
- (void) update; 

@end 

實現:

#import "SubOtherClass.h" 

@implementation SubOtherClass 

- (BOOL) acceptsFirstResponder{ 
    return true; 
} 

- (void) createVariables{ 
    self.classToUpdate = [[ClassToUpdate alloc] init]; 
    self.thingToRender1 = [[ThingToRender alloc] init]; 
    self.thingToRender2 = [[ThingToRender alloc] init]; 
} 

- (void) keyUp:(NSEvent *)theEvent{ 
    [super keyUp:theEvent]; 
    [self setNeedsDisplay:true]; 
} 

- (void) keyDown:(NSEvent *)theEvent{ 
    [super keyDown:theEvent]; 
} 

- (void) update{ 
    [self.classToUpdate update:self]; 
} 

- (void) drawRect:(NSRect)rect{ 
    [super drawRect:rect]; 
    [self update]; 
    [[NSColor blackColor] set]; 
    NSRectFill(rect); 
    [self.color1 set]; //this color is declared in superclass 
    NSString *str1 = [NSString stringWithFormat:@"%d %d %d %d", self.thingToRender1.x, self.thingToRender1.y, 30, 100]; 
    NSRectFill(NSRectFromString(str1)); 
    [self.color2 set]; //this color is declared in superclass 
    str1 = [NSString stringWithFormat:@"%d %d %d %d", self.thingToRender2.x, self.thingToRender2.y, 30, 100]; 
    NSRectFill(NSRectFromString(str1)); 
    [self.color3 set]; //this color is declared in superclass 
    str1 = [NSString stringWithFormat:@"%d %d %d %d", self.classToUpdate.x, self.classToUpdate.y, 30, 30]; 
    NSRectFill(NSRectFromString(str1)); 
} 

- (id)initWithFrame:(NSRect)frame 
{ 
    self = [super initWithFrame:frame]; 
    if (self) { 
    } 
    [self setNeedsDisplay:YES]; 
    return self; 
} 

@end 

OtherClass延伸的NSView

+1

你確定'self.classToUpdate'被正確賦值(即不是'nil')嗎?順便說一句,名稱選擇不是很好:我猜,它不是一個類,而是一個對象。如果你想調用一個類,'update'應該是一個類方法(不是對象方法)。 – Matthias

+0

這些名字只是個例子,它們不是我用過的真實名字,但下次我會嘗試使用更好的名字:) – Arc676

回答

0

我不太確定是什麼原因導致的問題,我覺得它很複雜,但我最終找到了解決方案。我將變量的構造函數移動到另一個方法,alloc永遠不會在SubOtherClass上調用。無論如何,感謝大家的幫助。

0

在你的drawRect方法只包括這條線: -

 [super drawrect:rect] 

因此,它調用超類的drawRect方法

1

你肯定self.classToUpdate在執行不是零? 也許你沒有在任何地方初始化該課程?

SubOtherClass與此代碼替換update方法:

- (void) update{ 
    if(!self.classToUpdate){ 
     NSLog(@"classToUpdate is nil"); 
    } 

    [self.classToUpdate update:self]; 
} 

並期待在控制檯上,如果「classToUpdate是零」的文本顯示

+0

謝謝,我沒有意識到變量是'nil'。我如何使它不零?當我添加'self.classToUpdate = [ClassToUpdate new]'Xcode告訴我「將保留的對象賦值給不安全的屬性;對象將在賦值後被釋放」。有什麼建議麼?我如何構造一個構造函數? – Arc676

+0

'self.classToUpdate = [[ClassToUpdate alloc] init]' – Axadiw

+0

我得到了同樣的警告......我在這裏錯過了什麼嗎?我需要在@屬性聲明中添加一些內容嗎?我有'分配'。 – Arc676

相關問題