我想組織某種程度上我的iPhone遊戲的水平視圖,但我根本無法(沒有展開對象分配)。我做了一個真正的「骨架」我的代碼(這個遊戲有2個關卡,目標是發佈iPhone顯示屏)。我不能釋放前一級,因此Instrunments會顯示增加BGTangramLevel實例。「交換」一個UIView實例變量 - 不能dealloc「先前」視圖
請看看它,我需要一些有用的想法設計(我的第三個問題)。
viewcontroller.h
@interface compactTangramViewController : UIViewController
{
//The level.
BGTangramLevel *currentLevel;
UIColor *levelColor;
}
//It is to be just a reference, therefore I use assign here.
@property (nonatomic, retain) BGTangramLevel *currentLevel;
-(void) notificationHandler: (NSNotification*) notification;
-(void) finishedCurrentLevel;
@end
viewcontroller.m
@implementation compactTangramViewController
@synthesize currentLevel;
//Initializer functions, setting up view hierarchy.
-(void) viewDidLoad
{
//Set up levelstepper.
levelColor = [UIColor greenColor];
//Set up "state" classes.
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(notificationHandler:) name:@"finishedCurrentLevel" object:nil];
//Attach level 1.
currentLevel = [BGTangramLevel levelWithColor: levelColor frame:self.view.frame];
[self.view addSubview:currentLevel];
[super viewDidLoad];
}
//Release objects.
-(void) dealloc
{
[currentLevel release];
[super dealloc];
}
//Notification handling.
-(void) notificationHandler: (NSNotification*) notification
{
//Execute level swap.
if ([notification name] == @"finishedCurrentLevel") [self finishedCurrentLevel];
}
-(void) finishedCurrentLevel
{
//Remove previous level.
[currentLevel removeFromSuperview];
//[currentLevel release];
//Step level.
if (levelColor == [UIColor greenColor]) levelColor = [UIColor blueColor]; else levelColor = [UIColor greenColor];
//Attach level 2.
currentLevel = [BGTangramLevel levelWithColor: levelColor frame:self.view.frame];
[self.view addSubview:currentLevel];
}
@end
BGTangramLevel.h
@interface BGTangramLevel : UIView
{
BOOL puzzleCompleted;
}
//Initializer.
+(BGTangramLevel*)levelWithColor: (UIColor*) color frame: (CGRect) frame;
//Test if the puzzle is completed.
-(void) isSolved;
@end
BGTangramLevel.m
@implementation BGTangramLevel
//Allocated instance.
+(BGTangramLevel*)levelWithColor: (UIColor*) color frame: (CGRect) frame
{
BGTangramLevel *allocatedLevel = [[BGTangramLevel alloc] initWithFrame:frame];
allocatedLevel.backgroundColor = color;
return allocatedLevel;
}
//Finger released.
-(void) touchesEnded: (NSSet*)touches withEvent: (UIEvent*)event
{
//The completement condition is a simple released tap for now...
puzzleCompleted = YES;
[self isSolved];
}
//Test if the puzzle is completed.
-(void) isSolved
{
//"Notify" viewController if puzzle has solved.
if (puzzleCompleted) [[NSNotificationCenter defaultCenter] postNotificationName:@"finishedCurrentLevel" object:nil];
}
-(void) dealloc
{
NSLog(@"Will ever level dealloc invoked."); //It is not.
[super dealloc];
}
@end
那麼我該怎麼做?我嘗試標記autorelease返回的級別實例,在removeFromSuperview後釋放currentLevel,嘗試以(非原子,分配)方式合成的currentLevel屬性,但Object Allocations仍在增長。我可以避免通知嗎?我卡住了。
聽起來很有希望。我期待着對它進行測試。是的,返回[allocLevel autorelease];由於事故而跳過。這些分配器/初始化器應該返回autorelease標記的對象,這很清楚。大多數答案建議使用合成訪問器,我相信我確實使用它們。我試圖自己實現訪問器以確保釋放/保留,但是沒用,因爲我從來沒有用簡單的currentLevel = [...賦值來調用它們。希望self.currentLevel = [...幫助。再次感謝。 – Geri 2009-12-02 08:57:41
還有一個問題。假設我得到/設置了槽訪問器,在調用removeFromSuperview(作爲文檔聲明)之前,我是否已經手動保留一次currentLevel?嗯...不要想,因爲我想讓BGTangramLevel實例在那裏釋放。 – Geri 2009-12-02 09:02:51
視圖保留其子視圖,所以'addSubview:'是一個隱式的'retain','removeFromSuperview'是一個隱式的'release'。 但是,由於您的控制器負責保留和釋放currentLevel,因此您不必關心其他人是否也這樣做。 該文檔只是指出,在不保留視圖的情況下調用'removeFromSuperview'是一個常見的錯誤,在這種情況下,它將立即解除分配。 – benzado 2009-12-03 08:20:18