我已经编写了一些代码,用于显示一个新视图(来自我的主视图控制器);然后它在关闭时调用主控制器,就像这样 -
-(void)showMyNewView {
MyNewViewController *myNewViewController = [[MyNewViewController alloc] initWithNibName:@"MyNewViewController" delegate:self];
[self.view addSubview:myNewViewController.view];
}
然后当新的关闭时,它会调用 -
-(void)myNewViewControllerDidFinish:(MyNewViewController *)myNewViewController {
[myNewViewController.view removeFromSuperview];
[myNewViewController release];
}
现在这个工作正常,并且没有泄漏,但是编译器呻吟着警告“在x行分配并存储到myNewViewController中的对象的潜在泄漏”。
我一直在看Apple的presentModalViewController:animated:代码,它也没有在创建它的方法中发布新的模态视图控制器,它似乎用dismissModalViewControllerAnimated释放它:当委托的viewControllerDidFinish:方法时调用叫做。这里有什么我想念的吗?使用presentModalViewController代码不会生成任何警告。非常感谢您的帮助。
答案 0 :(得分:0)
我想我现在已经弄明白了,而且我写了一小段代码,它给了我自己版本的“presentModalViewController:animated:”以及我想要的所有控件。我很高兴听到更多经验丰富的程序员对此做了什么(它可能非常直接,但我很久没有这样做了......),如果代码有任何问题,等等 -
接口:
#import <UIKit/UIKit.h>
enum {
MyViewLoaderTransitionTypeNone = 0,
MyViewLoaderTransitionTypeSomeEffect,
MyViewLoaderTransitionTypeSomeOtherEffect
};
typedef NSInteger MyViewLoaderTransitionType;
@interface MyViewLoader : UIViewController {
UIViewController *myLoadedViewController;
}
@property (nonatomic, retain) UIViewController *myLoadedViewController;
-(void)loadView:(UIViewController *)theViewController withTransition:(MyViewLoaderTransitionType)theTransition;
-(void)dismissViewWithTransition:(MyViewLoaderTransitionType)theTransition;
@end
实现:
#import "MyViewLoader.h"
@implementation MyViewLoader
@synthesize myLoadedViewController;
-(id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
;
}
return self;
}
-(void)dealloc {
[myLoadedViewController release];
[super dealloc];
}
-(void)loadView:(UIViewController *)theViewController withTransition:(MyViewLoaderTransitionType)theTransition {
[self setLoadedViewController:theViewController];
UIView *theLoadedView = theViewController.view;
[self.view addSubview:theLoadedView];
// do all sorts of transition stuff here
[theViewController viewWillAppear:NO];
}
-(void)dismissViewWithTransition:(MyViewLoaderTransitionType)theTransition {
UIView *theLoadedView = self.loadedViewController.view;
// do all sorts of transition stuff here
[theLoadedView removeFromSuperview];
self.loadedViewController = nil
}
我只是使用MyViewLoader作为我需要它的任何视图控制器的超类。
感谢您的任何意见/帮助!
答案 1 :(得分:-1)
这里通常要做的是,当您向视图添加子视图时,请在之后直接释放子视图。父视图负责子视图。稍后调用removeFromSuperview
时,会减少保留计数,并自动释放子视图。