2011-01-23 53 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