2011-01-23 30 views
0

目前我的應用程序使用時使用,當我在等待Web服務如何處理內存管理自定義模式對話框安裝/拆卸

@implementation AddModalDialog 

- (void)buildModalDialogWithTextForView:(NSString *)text:(UIViewController *)controller 
{ 
    UIView* _hudView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 400, 450)]; 
    _hudView.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.5]; 
    _hudView.clipsToBounds = YES; 

    UIActivityIndicatorView* _activityIndicatorView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge]; 
    _activityIndicatorView.frame = CGRectMake(140, 135, _activityIndicatorView.bounds.size.width, _activityIndicatorView.bounds.size.height); 
    [_hudView addSubview:_activityIndicatorView]; 
    [_activityIndicatorView startAnimating]; 

    UILabel* _captionLabel = [[UILabel alloc] initWithFrame:CGRectMake(30, 190, 250, 22)]; 
    _captionLabel.backgroundColor = [UIColor clearColor]; 
    _captionLabel.textColor = [UIColor whiteColor]; 
    _captionLabel.font = [UIFont systemFontOfSize:13.0]; 
    _captionLabel.adjustsFontSizeToFitWidth = NO; 
    _captionLabel.textAlignment = UITextAlignmentCenter; 
    _captionLabel.text = text; 
    [_hudView addSubview:_captionLabel]; 

    [controller.view addSubview:_hudView]; 
} 

- (void)removeModalDialogForView:(UIViewController *)controller 
{ 
    NSUInteger i, count = [controller.view.subviews count]; 
    [[controller.view.subviews objectAtIndex:(count - 1)] removeFromSuperview]; 
} 

@end 

我的問題是有關內存管理的自定義模式對話框對象這個對象。在上面的自定義UIView內可能會注意到的任何東西都是值得歡迎的,因爲它確實有改進的空間。

下面是我目前的工作瓦特/這在我的其他對象時,我想拉起模式

- (void)viewDidLoad 
{ 
    AddModalDialog* modal = [[AddModalDialog alloc] init]; 
    [modal buildModalDialogWithTextForView:@"Loading some details ..." :self]; 
    [modal release]; 
} 

然後Web服務後完成我通常稱之爲推倒

- (void)returnWebServiceDetails:(MyClass *)obj 
{ 
    AddModalDialog* modal = [[AddModalDialog alloc] init]; 
    [modal removeModalDialogForView:self]; 
    [modal release]; 
} 

我不應該初始化這個對象兩次,而是有一個屬性?新的obj-c開發人員正在尋找圍繞此行爲的最佳實踐。

預先感謝您

回答

2

首先,有效地轉移這些物品的所有權轉讓給控制器的視圖(因爲你一直沒有提及到他們周圍),所以你應該將它們添加到控制器的放行所有子視圖。其次,你不應該假設你知道控制器的視圖結構,而應該爲你的_hudView添加一些希望與應用程序中的任何其他內容不衝突的東西,並用它來檢索你的視圖。

第三,由於你根本沒有任何引用,所以這些會更好,而不是實例的類方法。沒有必要創建這個對象的實例,只是爲了讓它添加一些視圖並消失。

所以你相同的代碼,以下這三項準則,可能是這樣的:

@interface AddModalDialog { 
} 

+ (void)buildModalDialogWithText:(NSString *)text forController:(UIViewController *)controller; 
+ (void)removeModalDialogForController:(UIViewController *)controller; 

@end 

@implementation AddModalDialog 

// Class methods: use '+' instead of '-' 
+ (void)buildModalDialogWithText:(NSString *)text forController:(UIViewController *)controller 
{ 
    UIView* _hudView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 400, 450)]; 
    _hudView.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.5]; 
    _hudView.clipsToBounds = YES; 
    _hudView.tag = 2000; // use something that won't clash with tags you may already use 

    UIActivityIndicatorView* _activityIndicatorView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge]; 
    _activityIndicatorView.frame = CGRectMake(140, 135, _activityIndicatorView.bounds.size.width, _activityIndicatorView.bounds.size.height); 
    [_hudView addSubview:_activityIndicatorView]; 
    [_activityIndicatorView startAnimating]; 
    [_activityIndicatorView release]; // _hudView owns this now 

    UILabel* _captionLabel = [[UILabel alloc] initWithFrame:CGRectMake(30, 190, 250, 22)]; 
    _captionLabel.backgroundColor = [UIColor clearColor]; 
    _captionLabel.textColor = [UIColor whiteColor]; 
    _captionLabel.font = [UIFont systemFontOfSize:13.0]; 
    _captionLabel.adjustsFontSizeToFitWidth = NO; 
    _captionLabel.textAlignment = UITextAlignmentCenter; 
    _captionLabel.text = text; 
    [_hudView addSubview:_captionLabel]; 
    [_captionLabel release]; // _hudView owns this now 

    [controller.view addSubview:_hudView]; 
    [_hudView release]; // the controller's view owns this now 
} 

// Class methods: use '+' instead of '-' 
+ (void)removeModalDialogForController:(UIViewController *)controller 
{ 
    UIView* _hudView = [controller.view viewWithTag:2000]; 
    [_hudView removeFromSuperView]; // owned by the view, so we don't need to do anything more 
} 

@end 

而且你會使用它:

- (void)viewDidLoad 
{ 
    // Class methods, so we don't need to create an instance to use 
    [AddModalDialog buildModalDialogWithText:@"Loading some details..." forController:self]; 
} 

- (void)returnWebServiceDetails:(id)obj 
{ 
    // Class methods, so we don't need to create an instance to use 
    [AddModalDialog removeModalDialogForController:self]; 
} 
+0

哇 - 這是一些答案!感謝您爲此付出的所有努力! – 2011-01-24 01:12:17

2

buildModalDialogWithTextForView在它的底部,不要鬆開_activityIndicatorView_captionLabel_hudView - 你是這些的所有者(創建它們)。否則,他們只會泄漏。

更多Object Ownership and Disposal和的CoreFoundation Ownership Policy