在一個小型RTS項目中,我有一個按鈕列表,每個按鈕都有一個分配給它們的構建對象。有些建築非常通用,但其他建築非常特殊,我爲此將一些建築物作爲子類。ARC釋放子類NSObject,但不包含自定義NSObjects
當我做鈕釦,他們各自有一個PEHouse對象的列表,一切工作正常。
但是一旦這些PEHouses之一是子類PEHouse時,它會立即釋放,我不明白爲什麼。
按鈕具有這個屬性:
@property (nonatomic, strong) PEHouse *linkedHouse;
房子有下列值:
@interface PEHouse : NSObject {
NSString *title;
}
@property (nonatomic) BOOL is_locked;
我的塔有這樣的設置:當我產卵的列表
#import "PEHouse.h"
@interface PETower : PEHouse <NSObject>
-(void)secondAction;
+ (PETower*)createTower;
@end
像這樣的常規對象:
DLog(@"Building menu");
if ([key isEqualToString:@"button_income"]) {
[GMBuildMenu buttonFromBuilding:[PEHouse newSawmill] withIndex:1 inDict:dict];
[GMBuildMenu buttonFromBuilding:[PEHouse newQuarry] withIndex:2 inDict:dict];
[GMBuildMenu buttonFromBuilding:[PEHouse newIronMine] withIndex:3 inDict:dict];
[GMBuildMenu buttonFromBuilding:[PEHouse newMiscShop] withIndex:4 inDict:dict];
[GMBuildMenu buttonFromBuilding:[PEHouse newWeaponsShop] withIndex:5 inDict:dict];
[GMBuildMenu buttonFromBuilding:[PEHouse newArmorShop] withIndex:6 inDict:dict];
[GMBuildMenu buttonFromBuilding:[PEHouse newBazaar] withIndex:7 inDict:dict];
[GMBuildMenu buttonFromBuilding:[PEHouse newTreasury] withIndex:8 inDict:dict];
return YES;
}
一切正常。當我釀出這樣的我子集建築按鍵只有一個:
if ([key isEqualToString:@"button_military"]) {
[GMBuildMenu buttonFromBuilding:[PEHouse newTower] withIndex:1 inDict:dict];
}
的對象後立即釋放...
有沒有人有一個線索,爲什麼這種分化發生?
更新
的代碼看起來是這樣的:
+(OpenGLButton*)buttonFromBuilding:(PEHouse*)house withIndex:(int)index inDict:(NSMutableDictionary*)dict {
OpenGLButton *new = [GMButtonMenu addButtonToMenu:dict withLabel:house.button_art andShowname:house.title];
new.linkedHouse = house;
CGRect buttonFrame = CGRectMake(20, ((30)*index), (28*[GMButtonMenu globalGUIScale]), (28*[GMButtonMenu globalGUIScale]));
new.location = buttonFrame;
new.index = index;
new.details = house.desc;
return new;
}
在PEHouse我們發現:
+ (PETower *)newTower {
PETower *new = [PETower createTower];
return new;
}
+ (PEHouse *)newSawmill {
PEHouse *new = [PEHouse createDefault];
new.title = @"Sawmill";
new.is_large = YES;
return new;
}
'buttonFromBuilding:withIndex:inDict:'看起來像什麼?按照普通的Cocoa約定,它應該只是返回一個非擁有引用的按鈕,但我明白你用它來做別的事情了嗎? – Tommy
你可以粘貼這兩種方法:'sawMill'和'newTower'? – Levi
按要求添加。 –