我想在我的应用程序的每个屏幕上显示一个自定义栏,并带有可用的按钮。我在 init 方法中将 CustomViewController 添加到我的类中,并且一切正常,除非我分析我的应用程序时出现潜在的内存泄漏。
当我释放 [customViewController release] 时,CustomViewController 上的按钮将不再起作用。在没有内存泄漏的情况下实施此解决方案的正确方法是什么。
#import "CustomViewController.h"
@implementation CustomViewController
- (IBAction)doSomething:(id)sender
{
// Perform an action
}
@end
我创建 CustomViewController 的 ViewController:
- (id)initWithNibName:(NSString *)nibNameOrNil
bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
CustomViewController *customViewController = [[CustomViewController alloc] initWithNibName:@"CustomViewController" bundle:nil];
UIView *bar = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 30)];
[bar addSubview:customViewController.view];
[self.view addSubview:bar];
[bar release];
}
}